I know many questions have been asked and answered, but I'm still facing this issue. Maybe someone can point out where the mistake is being made.
I tried to compare two Objects in JavaScript but it isn't working for me.
This is the code I tried.
console.log('typeof Object1', typeof val);
console.log('Object1', val);
console.log('Object1 {...val}', {...val});
console.log('Object.getOwnPropertyNames(Object1)', Object.getOwnPropertyNames(val));
console.log('Object.entries(Object1)', Object.entries(val));
console.log('Object.keys(Object1)', Object.keys(val));
console.log('Object.values(Object1)', Object.values(val));
console.log('getClass(Object1)', getClass(val));
console.log('----------------------------------------');
console.log('typeof Object2', typeof divElemStyles[property][index]);
console.log('Object2', divElemStyles[property][index]);
console.log('Object1 {...Object2)}', {...divElemStyles[property][index]});
console.log('Object.getOwnPropertyNames(Object2)', Object.getOwnPropertyNames(divElemStyles[property][index]));
console.log('Object.entries(Object2)', Object.entries(divElemStyles[property][index]));
console.log('Object.keys(Object2)', Object.keys(divElemStyles[property][index]));
console.log('Object.values(dObject2)', Object.values(divElemStyles[property][index]));
console.log('getClass(Object2)', getClass(divElemStyles[property][index]));
//lodash
console.log('_.isEqual(Object1, Object2)', _.isEqual(val, divElemStyles[property][index]));
//underscore
console.log('_.isEqual(Object1, Object2)', _.isEqual(val, divElemStyles[property][index]));
//native
console.log('Object.is(Object1, Object2)', Object.is(val, divElemStyles[property][index]));
//equal sign
console.log('Object1 === Object2', val === divElemStyles[property][index]);
Considering both Object1 and Object2 hold the same property, comparing them should result in True, but instead, I'm getting False.
You can also notice this, both Objects are instances of the CSSKeywordValue
class. Also using with Object.entries() return empty array. Weird for me.
Here is how you can regenerate this issue. Please see the result in chrome console, instead of codepen or jsfiddle.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.div1 {
display: flex;
}
.div2 {
display: block;
}
</style>
</head>
<body>
<div class="div1">
</div>
<div class="div2">
</div>
<script>
function getClass(obj) {
// if the type is not an object return the type
let type = typeof obj
if((type !== 'object')) {
return type;
} else { //otherwise, access the class using obj.constructor.name
return obj.constructor.name;
}
}
var elem1 = document.querySelector('.div1');
var elemStyles1 = {};
for (const [prop, val] of elem1.computedStyleMap()) {
elemStyles1[prop] = val;
}
var elem2 = document.querySelector('.div2');
var elemStyles2 = {};
for (const [prop, val] of elem2.computedStyleMap()) {
elemStyles2[prop] = val;
}
var Object1 = elemStyles1['display'][0];
var Object2 = elemStyles2['display'][0];
console.log('typeof Object1', typeof Object1);
console.log('Object1', Object1);
console.log('Object1 {...Object1}', {...Object1});
console.log('Object.getOwnPropertyNames(Object1)', Object.getOwnPropertyNames(Object1));
console.log('Object.entries(Object1)', Object.entries(Object1));
console.log('Object.keys(Object1)', Object.keys(Object1));
console.log('Object.values(Object1)', Object.values(Object1));
console.log('getClass(Object1)', getClass(Object1));
console.log('----------------------------------------');
console.log('typeof Object2', typeof Object2);
console.log('Object2', Object2);
console.log('Object2 {...Object2}', {...Object2});
console.log('Object.getOwnPropertyNames(Object2)', Object.getOwnPropertyNames(Object2));
console.log('Object.entries(Object2)', Object.entries(Object2));
console.log('Object.keys(Object2)', Object.keys(Object1));
console.log('Object.values(Object2)', Object.values(Object2));
console.log('getClass(Object2)', getClass(Object2));
//native
console.log('Object.is(Object1, Object2)', Object.is(Object1, Object2));
//equal sign
console.log('Object1 === Object2', Object1 === Object2);
//Json Stringfy
console.log('JSON.stringify(Object1) === JSON.stringify(Object2)', JSON.stringify(Object1) === JSON.stringify(Object2));
</script>
</body>
</html>