简体   繁体   中英

Lodash isEqual - How to get keys of objects that differ

I'm trying to obtain an array of keys, that were different as a result of comparing two objects. I was only able to do it using isEqualWith and mapKeys , like so:

 const differencies = []; const objA = { a: 1, b: 2, c: 3 }; const objB = { a: 1, b: 2, c: 100 }; function customizer(objOne, objTwo) { if (lodash.isEqual(objOne, objTwo)) { return true; } lodash.mapKeys(objOne, (value, key) => { if (value !== objTwo[key]) { differencies.push(key); } }); return false; } lodash.isEqualWith(objA, objB, customizer); console.log(differencies);
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script> <script>const lodash = _;</script>

Question is : Is there a better way to get keys of objects that differ using lodash?

Since you only need to compare objects where the keys are identical but values might differ, you can leverage keys() or keysIn() (if you want to also traverse the prototype chain) and remove all entries that do not have a matching value with filter() .

 const objA = { a: 1, b: 2, c: 4 }; const objB = { a: 1, b: 2, c: 100 }; const differencies = lodash.filter( lodash.keys(objA), key => objA[key] !== objB[key] ); console.log(differencies);
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script> <script>const lodash = _;</script>

Alternatively using chaining syntax :

 const objA = { a: 1, b: 2, c: 4 }; const objB = { a: 1, b: 2, c: 100 }; const differencies = lodash(objA) .keys() .filter(key => objA[key] !== objB[key]); console.log(differencies);
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script> <script>const lodash = _;</script>

For this sort of functionality, Lodash might be an overkill. Simple JavaScript equivalent is Object.keys() and Array#filter() :

 const objA = { a: 1, b: 2, c: 4 }; const objB = { a: 1, b: 2, c: 100 }; const differencies = Object.keys(objA) .filter(key => objA[key] !== objB[key]); console.log(differencies);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM