1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-11-10 00:09:06 +01:00

problems(js): add “2628. JSON Deep Equal”

Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
Matej Focko 2023-06-03 23:52:07 +02:00
parent 5253091624
commit 060090921d
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -0,0 +1,38 @@
/**
* @param {any} o1
* @param {any} o2
* @return {boolean}
*/
var areDeeplyEqual = function(o1, o2) {
// passes strict equality
if (o1 === o2) {
return true;
}
// one is null
if (o1 === null || o2 === null) {
return false;
}
// different string representations
if (String(o1) != String(o2)) {
return false;
}
// if it's not an object we can decide with strict equality
if (typeof o1 !== 'object') {
return o1 === o2;
}
if (Array.isArray(o1)) {
return (
o1.length === o2.length
&& Array.from(o1.keys()).every(i => areDeeplyEqual(o1[i], o2[i]))
);
}
return (
Object.keys(o1).length === Object.keys(o2).length
&& Object.keys(o1).every(key => areDeeplyEqual(o1[key], o2[key]))
);
};