My task involves comparing two large JavaScript Objects, each containing approximately 200 keys. The keys in the objects are unordered and have different values, but I am only interested in whether they share the same set of keys. If both objects have the same keys, the function should return true regardless of their order.
I attempted to use the following code, but it did not work as expected:
var compareObjects = function (object1, object2) {
let keysObj1 = Object.keys(object1);
let keysObj2 = Object.keys(object2);
for (key in keysObj1){
if (!keysObj2.includes(key)){
console.log("Missing key in object2: ", key);
}
}
}
Example:
{"key1": "val", "key2": "sdsfaf"}
{"Key2": "val", "key1": "vsdsdsd"}
This should return true
{"key1": "val", "key2": "sdsfaf"}
{"Key2": "val"}
This shouldn't