Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
428 views
in Technique[技术] by (71.8m points)

javascript - 如何使用lodash对2个对象进行深入比较?(How to do a deep comparison between 2 objects with lodash?)

I have 2 nested objects which are different and I need to know if they have difference in one of their nested properties.

(我有2个不同的嵌套对象,我需要知道它们的嵌套属性之一是否有所不同。)

var a = {};
var b = {};

a.prop1 = 2;
a.prop2 = { prop3: 2 };

b.prop1 = 2;
b.prop2 = { prop3: 3 };

The object could be much more complex with more nested properties.

(具有更多嵌套属性的对象可能要复杂得多。)

But this one is a good example.

(但这是一个很好的例子。)

I have the option to use recursive functions or something with lodash...

(我可以选择使用递归函数或带有lodash的东西...)

  ask by JLavoie translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

An easy and elegant solution is to use _.isEqual , which performs a deep comparison:

(一个简单而优雅的解决方案是使用_.isEqual ,它执行深度比较:)

var a = {};
var b = {};

a.prop1 = 2;
a.prop2 = { prop3: 2 };

b.prop1 = 2;
b.prop2 = { prop3: 3 };

_.isEqual(a, b); // returns false if different

However, this solution doesn't show which property is different.

(但是,此解决方案未显示哪个属性不同。)

http://jsfiddle.net/bdkeyn0h/

(http://jsfiddle.net/bdkeyn0h/)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...