data = {
json: 'is life'
};
anoth = JSON.parse(JSON.stringify(data));
if (data == anoth){
console.log("yes")
}else{
console.log("nah")
}
console.log(data, anoth)
They are obviously equal but why doesnt it work in the code
data = {
json: 'is life'
};
anoth = JSON.parse(JSON.stringify(data));
if (data == anoth){
console.log("yes")
}else{
console.log("nah")
}
console.log(data, anoth)
They are obviously equal but why doesnt it work in the code
Because you are comparing object references against each other. When you deserialized the original serialized JSON object, a new and different object was returned. The two have the same content, but they are different object instances. If you compare the JSON.stringify() versions you will get a match.
data = {
json: 'is life'
};
anoth = JSON.parse(JSON.stringify(data));
if (data == anoth){
alert("Objects are same.")
}else{
alert("Objects are not same.")
}
if (JSON.stringify(data) == JSON.stringify(anoth)){
alert("Content is same")
}else{
alert("Content is not same.")
}
alert(JSON.stringify(data) + "\n" + JSON.stringify(anoth))
You should compare two objects for equality but in your example you only compare references Object comparison in JavaScript