Since you want to compare two objects on limited keys, either you need to specifically mention each key in if condition or you need to maintain separate memory for those keys. If you have less excluding keys then maintain an array for that else for including keys.
const baseObject = {
firstName: "John",
lastName: "Doe",
dob: "01/01/00",
siblings: []
}
const updatedObject = {
firstName: "Johnathan",
lastName: "Doe",
dob: "01/01/00",
siblings: []
}
// Excluding
function compareWithoutKeys(first = {}, second = {}, excludes = []) {
return Object.entries(first).every(([key, value]) =>
excludes.includes(key) ? true : second[key] === value
)
}
compareWithoutKeys(baseObject, updatedObject, ['dob', 'siblings']) // true/false
// Including
function compareWithKeys(first = {}, second = {}, includes = []) {
return Object.entries(first).every(([key, value]) =>
includes.includes(key) ? second[key] === value : true
)
}
compareWithKeys(baseObject, updatedObject, ['firstName', 'lastName']) // true/false
Update for comparing more than just strings.
In case you want to compare more than string maybe like siblings(which is an array) you have to update the comparison function, you will check the type of value and then compare it accordingly. Like if the value is an Object then compare every key or if the value is Array then compare every index. Something like:
function isEqual(first, second) {
const firstType = Object.prototype.toString.call(first)
const secondType = Object.prototype.toString.call(second)
if (firstType !=== secondType) {
return false
}
switch (expression) {
case '[object Array]': return first.every((value, index) => value === second[index])
case '[object Object]': return Object.entries(first).every((value, index) => value === second[index])
default: return first === second
}
}
// Excluding
function compareWithoutKeys(first = {}, second = {}, excludes = []) {
return Object.entries(first).every(([key, value]) =>
excludes.includes(key) ? true : isEqual(value, second[key])
)
}
// usage
compareWithoutKeys(baseObject, updatedObject, ['dob', 'siblings']) // true/false
compareWithoutKeys(baseObject, updatedObject, ['dob']) // true/false
// Including
function compareWithKeys(first = {}, second = {}, includes = []) {
return Object.entries(first).every(([key, value]) =>
includes.includes(key) ? isEqual(value, second[key]) : true
)
}
// usage
compareWithKeys(baseObject, updatedObject, ['firstName', 'lastName']) // true/false
compareWithKeys(baseObject, updatedObject, ['firstName', 'lastName', 'siblings']) // true/false
Note: This will only do first level of comparison, if you want to do deep levels comparison like maybe array of objects on which every key is an array, either you have to use a recursive function or fallback to libraries like lodash.
A recursive could be something like following, but I haven't tested the below code.
function isEqual(first, second) {
const firstType = Object.prototype.toString.call(first)
const secondType = Object.prototype.toString.call(second)
if (firstType !=== secondType) {
return false
}
switch (expression) {
case '[object Array]': return first.every((value, index) => isEqual(second[index], value))
case '[object Object]': return Object.entries(first).every((value, index) => isEqual(second[index], value))
default: return first === second
}
}
Article on recursive comaprison: https://gomakethings.com/check-if-two-arrays-or-objects-are-equal-with-javascript/
Lodash's isEqual: https://lodash.com/docs/4.17.10#isEqual
if (a.firstName == b.firstName && a.lastName == b.lastName)?