LeetCode/js/differences-between-two-objects.js
Matej Focko 2351dfd0ee
chore: unwrap one layer
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-12-12 14:36:00 +01:00

29 lines
631 B
JavaScript

/**
* @param {object} obj1
* @param {object} obj2
* @return {object}
*/
function objDiff(obj1, obj2) {
if (obj1 === obj2) {
return {};
}
if (
obj1 === null || obj2 === null
|| typeof obj1 !== 'object' || typeof obj2 !== 'object'
|| Array.isArray(obj1) !== Array.isArray(obj2)
) {
return [obj1, obj2];
}
let result = {};
for (let key in obj1) {
if (key in obj2) {
let diff = objDiff(obj1[key], obj2[key]);
if (Object.keys(diff).length) {
result[key] = diff;
}
}
}
return result;
};