I have a pretty naive way to compare a subset of two objects' properties with each other. It gets hard to read when there are many properties in the subset. What could be some alternatives?
const obj1 = {a: 22, b: 33, c: 44};
const obj2 = {a: 22, b: 34, c: 44};
JSON.stringify([obj1.a, obj1.b]) === JSON.stringify([obj2.a, obj2.b])
Subset comparison : SubsetCompare
function does a flat comparison of a subset of two objects.
let obj1 = {a: 22, b: 34, c: 44}; let obj2 = {a: 22, b: 34, c: 10}; let subsetCompare = (obj1, obj2, keys) => keys.every(key => obj1[key] === obj2[key]); console.log(subsetCompare(obj1, obj2, ['a', 'b'])); console.log(subsetCompare(obj1, obj2, ['a', 'b', 'c']))
Flat comparison : flatCompare
function only compares value of primitive type members and reference of reference type members.
let x = { a : 10}; let y = { a : 10}; let obj1 = {a: 22, b: 34, c: 44, d:y}; let obj2 = {a: 22, b: 34, c: 44, d:x}; let flatCompare = (obj1, obj2) => { let keys = []; if((keys = getKeys(obj1)).length != getKeys(obj2).length) return false; return keys.every(key => obj1[key] === obj2[key]); } let isObject = (obj) => obj === Object(obj); let getKeys = (obj) => Object.getOwnPropertyNames(obj); console.log(flatCompare(obj1, obj2));
Deep comparison : deepCompare
function recursively compares also values of primitive type members of nested objects.
let x = { a : 10}; let y = { a : 10}; let obj1 = {a: 22, b: 34, c: 44, d:y}; let obj2 = {a: 22, b: 34, c: 44, d:x}; let deepCompare = (obj1, obj2) => { let keys = []; if((keys = getKeys(obj1)).length != getKeys(obj2).length) return false; return keys.every(key => { if(isObject(obj1[key]) && isObject(obj2[key])) return deepCompare(obj1[key], obj2[key]); return obj1[key] === obj2[key]; }); }; let isObject = (obj) => obj === Object(obj); let getKeys = (obj) => Object.getOwnPropertyNames(obj); console.log(deepCompare(obj1, obj2))
Why not use a function?
let obj1 = {a: 22, b: 33, c: 44}; let obj2 = {a: 22, b: 34, c: 44}; console.log(compareObjects(obj1, obj2, [`a`, `b`])); // false console.log(compareObjects(obj1, obj2, [`a`, `c`])); // true console.log(compareObjects(obj1, obj2, [`b`, `c`])); // false function compareObjects(obj1, obj2, keys) { let i; for (i = keys.length - 1; i > -1 && obj1[keys[i]] === obj2[keys[i]]; i--); return i === -1; // if i equals -1 it went through the whole loop and did not fail for the second condition }
Based on @revilheart's answer but with @Ryan's every
suggestion:
let obj1 = {a: 22, b: 33, c: 44}; let obj2 = {a: 22, b: 34, c: 44}; console.log(compareObjects(obj1, obj2, [`a`, `b`])); // false console.log(compareObjects(obj1, obj2, [`a`, `c`])); // true console.log(compareObjects(obj1, obj2, [`b`, `c`])); // false function compareObjects(obj1, obj2, keys) { return keys.every(key => obj1[key] === obj2[key]) }
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.