How can I do a shallow comparison of the properties of two objects with Javascript or lodash?

后端 未结 8 1932
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-01 11:31

Is there a way I can do a shallow comparison that will not go down and compare the contents of objects inside of objects in Javascript or lodash? Note that I did check lodas

8条回答
  •  甜味超标
    2021-01-01 12:23

    keeping in mind that it only for shallow and only for strings and numbers

    function equals(obj1, obj2) {
      return Object.keys(obj1)
        .concat(Object.keys(obj2))
        .every(key => {
          return obj1[key] === obj2[key];
        });
    }
    

提交回复
热议问题