I'm trying to compare two objects. I use socket.io to send the nodes
object to the client.
My server.js
goes:
var nodes = {'101':{ID:'101',x:100,y:200},
'102':{ID:'102',x:200,y:200}};
socket.emit('message', nodes);
In my client, I'd like to create a copy of the object nodes
only for the first time it receives. Save it to the variable oldNodes
and the next time the socket receives the nodes
object, do a comparison with oldNodes
and output which property has been changed.
E.g., if it receives var nodes = {'101':{ID:'101',x:100,y:200},'102':{ID:'102',x:200,y:300}};
,
it should print(console.log) that nodes[102].y is changed
.
client.html
:
socket.on('message', function(nodes){
if (oldNodes == undefined){
var oldNodes = nodes;
}
else{
//compare oldNodes & nodes
//print result
}
});
For copying, var oldNodes = nodes;
is not what I want since javascript passes objects as a reference. So, even if oldNodes & nodes
have the same content, (oldNodes == nodes)
will produce a false
. How do I copy the object and COMPARE its properties efficiently?