Get the property of the difference between two objects in javascript

China☆狼群 提交于 2019-12-03 09:14:59

You could filter the keys (assuming same keys) by checking the unequal value.

var obj1 = { prop1: false, prop2: false, prop3: false },
    obj2 = { prop1: false, prop2: true, prop3: false },
    difference = Object.keys(obj1).filter(k => obj1[k] !== obj2[k]);
    
console.log(difference);

this is the fastest and simplest method

var a ={
  prop1: false,
  prop2: false,
  prop3: false
}

var b={
  prop1: false,
  prop2: true,
  prop3: false
}

 JSON.stringify(a) === JSON.stringify(b) 

this return false. order of props is also important

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!