吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1118|回复: 8
收起左侧

[求助] JS比较对象的问题

[复制链接]
cqwcns 发表于 2019-12-26 12:45
本帖最后由 cqwcns 于 2019-12-26 12:53 编辑

以下函数用于比较两个对象,并返回差异的部分。

[JavaScript] 纯文本查看 复制代码
	
function funDifference(object, other) {
	let diff = {};
	let vChildren;
	for (var key in object) {
		if (typeof object[key] === "object" && typeof other[key] === "object" && object[key] && other[key]) {
			vChildren = funDifference(object[key], other[key]);
			if (Object.keys(vChildren).length > 0) {
				diff[key] = vChildren;
			}
		} else if (object[key] !== other[key]) {
			diff[key] = object[key];
		}
		
	}
	return diff;
}

var object1 = {firstname: 'John', lastname: 'Cena', privateInfo: {privateProperty1: false, privateProperty2: ['20%','600%']}};
var object2 = {firstname: 'John', lastname: 'Csena', privateInfo: {privateProperty1: true, privateProperty2:['20%','60%']}};
console.log(JSON.stringify(funDifference(object2, object1)));


问题在于函数返回的是:
[JavaScript] 纯文本查看 复制代码
{"lastname":"Csena","privateInfo":{"privateProperty1":true,"privateProperty2":{"1":"60%"}}}

我希望返回的是:
[JavaScript] 纯文本查看 复制代码
 {"lastname":"Csena","privateInfo":{"privateProperty1":true,"privateProperty2":["20%","60%"]}}


函数要怎么改?谢谢

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

夕阳啊丶 发表于 2019-12-26 13:18
你要返回整个数组还是 差异的具体值
夕阳啊丶 发表于 2019-12-26 13:28
本帖最后由 夕阳啊丶 于 2019-12-26 13:29 编辑
funDifference(object, other) {
        let diff = {};
        let vChildren;
        for (let key in object) {
          if (typeof object[key] === "object" && typeof other[key] === "object" && object[key] && other[key]) {
            if (Object.prototype.toString.call(object[key]) === '[object Array]') {
              vChildren = object[key]
            } else {
              vChildren = this.funDifference(object[key], other[key]);
            }
            if (Object.keys(vChildren).length > 0) {
              diff[key] = vChildren;
            }
          } else if (object[key] !== other[key]) {
            diff[key] = object[key];
          }
        }
        return diff;
      },
exp:
    let object1 = {
        firstname: 'John',
        lastname: 'Cena',
        privateInfo: {
          privateProperty1: false,
          privateProperty2: ['20%', '600%', 'test_test', ['one', 'two', 'three', ['one1', 'two1', 'three2']], 'hahaha',{Obj1:{a:1,b:2}}],
          test: [1, 2, 3, [11, 22, 33]]
        }
      };
      let object2 = {
        firstname: 'John',
        lastname: 'Csena',
        privateInfo: {
          privateProperty1: true,
          privateProperty2: ['20%', '60%', 'test_test', ['one', 'two', 'three', ['one1', 'two1', 'three1']], 'hahaha',{Obj1:{a:1,b:4}}],
          test: [1, 2, 3, [11, 22, 44]]
        }
      };

      console.log(this.funDifference(object2, object1));
[JavaScript] 纯文本查看 复制代码
funDifference(object, other) {
        let diff = {};
        let vChildren;
        for (let key in object) {
          if (typeof object[key] === "object" && typeof other[key] === "object" && object[key] && other[key]) {
            if (Object.prototype.toString.call(object[key]) === '[object Array]') {
              vChildren = object[key]
            } else {
              vChildren = this.funDifference(object[key], other[key]);
            }
            if (Object.keys(vChildren).length > 0) {
              diff[key] = vChildren;
            }
          } else if (object[key] !== other[key]) {
            diff[key] = object[key];
          }
        }
        return diff;
      },
夕阳啊丶 发表于 2019-12-26 13:30
不知道能不能满足你的需求 其实还有其他情况 例如方法 map 不一一处理了
wei125483 发表于 2019-12-26 14:03
你要的是{"lastname":"Csena","privateInfo":{"privateProperty1":true,"privateProperty2":["20%","60%"]}}
还是{"lastname":"Csena","privateInfo":{"privateProperty1":true,"privateProperty2":["60%"]}}
 楼主| cqwcns 发表于 2019-12-26 15:13
wei125483 发表于 2019-12-26 14:03
你要的是{"lastname":"Csena","privateInfo":{"privateProperty1":true,"privateProperty2":["20%","60%"]} ...

要的是第一个
wei125483 发表于 2019-12-26 15:33

[JavaScript] 纯文本查看 复制代码
function funDifference(object, other) {
    let diff = {};
    let vChildren;
    for (var key in object) {
        if (typeof object[key] === "object" && typeof other[key] === "object" && object[key] && other[key]) {
            vChildren = funDifference(object[key], other[key]);
            if (Object.keys(vChildren).length > 0) {
				if(object instanceof Array)
					diff = [vChildren];
				else
                	diff[key] = vChildren;
            }
        } else if (object[key] !== other[key]) {
			diff = object;
            diff[key] = object[key];
        }
         
    }
    return diff;
}
 
var object1 = {firstname: 'John', lastname: 'Cena', privateInfo: {privateProperty1: false, privateProperty2: ['20%','600%']}};
var object2 = {firstname: 'John', lastname: 'Csena', privateInfo: {privateProperty1: true, privateProperty2:['20%','60%']}};
console.log(JSON.stringify(funDifference(object2, object1)));
wei125483 发表于 2019-12-26 15:36
wei125483 发表于 2019-12-26 15:33
[mw_shl_code=javascript,true]function funDifference(object, other) {
    let diff = {};
    let  ...

有些有缺陷,细化的可以自己去弄吧
 楼主| cqwcns 发表于 2019-12-27 13:30


功能已经实现,感觉代码写得有些冗余,请各位大佬指正。
[JavaScript] 纯文本查看 复制代码
function funDifference(object, other) {
	let diff = {};
	let vChildren;
	for (var key in object) {
		if (Object.prototype.toString.call(object[key]) === '[object Object]' && Object.prototype.toString.call(other[key]) === '[object Object]' && object[key] && other[key]) {

			vChildren = funDifference(object[key], other[key]);
			if (Object.keys(vChildren).length > 0) {
				diff[key] = vChildren;
			}
		} else if (Object.prototype.toString.call(object[key]) === '[object Array]' && Object.prototype.toString.call(other[key]) === '[object Array]' && object[key] && other[key]) {

			$.each(object[key], function (i, v) {

				if (typeof v === "object") {
					vChildren = funDifference(object[key], other[key]);
					if (Object.keys(vChildren).length > 0) {
						diff[key] = vChildren[0];
					}

				} else if (JSON.stringify(object[key].sort()) !== JSON.stringify(other[key].sort())) {
					diff[key] = object[key];
				}

			})

		} else if (object[key] !== other[key]) {
			diff[key] = object[key];
		}

	}
	return diff;
}

var object = {firstname: ['John','kks'], lastname: 'Cena',tel: '33',privateInfo: {privateProperty1: "false", privateProperty2: [{oo:"sd",aa:[{yy:'88',ll:'3s3'}]}]}};
var other = {firstname: ['John','kk'], lastname: 'Cena', tel: '44',privateInfo: {privateProperty1: "true", privateProperty2: [{oo:"sd",aa:[{yy:'88',ll:'33'}]}]}};

console.log(JSON.stringify(funDifference(object, other)));



输出:
[JavaScript] 纯文本查看 复制代码
{"firstname":["John","kks"],"tel":"33","privateInfo":{"privateProperty1":"false","privateProperty2":{"aa":{"ll":"3s3"}}}}
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-1-13 14:48

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表