1
var x = {name:'james',email:'james@j.com',tel:999};
var y = {name:'james',email:'james@j.com',tel:991};

How can I compare above x and y using loop? I'm expecting false because tel is not equal.

tried for in but found it's too messy to maintain later. Sad I couldn't use jquery.

Thian Kian Phin
  • 921
  • 3
  • 13
  • 25
  • [How to determine equality for two JavaScript objects?](http://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects) – adeneo Apr 24 '16 at 17:04

1 Answers1

0

Assuming you have the same properties in both objects, like you do above you could do this:

JsBin Example

var x = {name:'james',email:'james@j.com',tel:999};
var y = {name:'james',email:'james@j.com',tel:991};

for (var prop in x) {
  if (x[prop] !== y[prop]) {
    // do stuff here .. return false;
  }
}
omarjmh
  • 13,632
  • 6
  • 34
  • 42