LeetCode/problems/js/json-deep-equal.js
Matej Focko 060090921d
problems(js): add “2628. JSON Deep Equal”
Signed-off-by: Matej Focko <mfocko@redhat.com>
2023-06-03 23:52:07 +02:00

38 lines
847 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @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]))
);
};