JS Tutorial: Find if Two Object Values are Equal to Each Other
Why Stack Overflow recommends it
Stack Overflow users link it in answers about comparing objects or JSON, where its approach is adapted into an isEqual function for deep or shallow value comparison.
Recommended by 3 people across 3 different months, 2020 to 2021.
What it is
A short JavaScript tutorial that explains why comparing two objects with === or == returns false, then builds a function that checks whether two objects hold equal values. It walks through key counts, value comparison and recursion for nested objects, presented by the edutechional channel.
What people said
Recommends Amjad Abujamous · 2020-03 · Stack Overflow · on “How to compare two JSON have the same properties without order?”
I adapted and modified the code from this tutorial [this video] to write a function that does a deep comparison of two JS objects. const isEqual = function(obj1, obj2) { const obj1Keys = Object.keys(obj1); const obj2Keys = Object.keys(obj2); if(obj1Keys.length !== obj2Keys.length) { return false; } for (let objKey of obj1Keys) { if (obj1[objKey] !== obj2[objKey]) { if(typeof obj1[objKey] == "object" && typeof obj2[objKey] == "object") { if(!isEqual(obj1[objKey], obj2[objKey])) { return false; } } else { return false; } } } return true; }; The function compares the respective values of the same keys for the two objects. Further, if the two values are objects, it uses recursion to execute deep comp
Recommends Daniel Ocando · 2021-01 · Stack Overflow · on “How to compare two different Firestore queries?”
To check the values of the contents of your objects are the same you could adapt this code [LINK] (there is also a video [this video] version) as per your needs and check your queries. Let me know if you find this information to be useful.
Recommends Abdo Hassan · 2021-03 · Stack Overflow · on “How can I do a shallow comparison of the properties of two objects with Javascript or lodash?”
const isEqual = (a, b) => { // compare keys const xKeys = Object.keys(a); const bKeys = Object.keys(b); if (xKeys.length !== bKeys.length) { return false; } // compare values for (let objKeys in xKeys) { if (xKeys[objKeys !== bKeys[objKeys]]) { return false; } } return true; }; var a = { x: 1, y: 2, }; var b = { x: 1, y: 2, }; console.log(isEqual(a, b)); // true And you can see this video is very helpful for your question : JS Tutorial: Find if Two Object Values are Equal to Each Other [this video]