mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-10 00:09:06 +01:00
problems(js): add “2700. Differences Between Two Objects”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
799bc6f803
commit
5253091624
1 changed files with 29 additions and 0 deletions
29
problems/js/differences-between-two-objects.js
Normal file
29
problems/js/differences-between-two-objects.js
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
/**
|
||||||
|
* @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;
|
||||||
|
};
|
Loading…
Reference in a new issue