0

I have asked similar question, but this one is about object.

Let say I have 2 objects

firstObject  = {0: 1, 1: 2, 2: 3, 4: 4, 4: 5};
secondObject = {0: 5, 1: 4, 2: 3, 4: 2, 4: 1};

I want to know if they contain the same property values, while order and property name is not important. (The function should return true for the example above.) Translating the object to array is NOT an option.

I know I can write a function to sort them and then loop through them to check, but is there a pre-built function for this?

P.S. I have noticed the possible duplicated question, but that post only consider if the object is equal under the same property name, while you can see in my example that I am only considering the property value but not the property names.

Community
  • 1
  • 1
cytsunny
  • 4,838
  • 15
  • 62
  • 129
  • Yes, there is. Have a look at underscore or lodash. *edit:* on second read, you only seem to care about the property values, not the names. Not sure about these libraries, but someone on the Internet surely wrote something for that. – Felix Kling Apr 16 '15 at 08:05
  • 1
    Please make it clearer that you are only interested in the property values, not the property names. "Order" is the wrong word for that. Different order would be `{foo:..., bar:...}` vs `{bar:..., foo:...}` – Felix Kling Apr 16 '15 at 08:23
  • Order isn't guaranteed for object keys. – joews Apr 16 '15 at 08:23
  • @joews: irrelevant to the question, the OP is just using the wrong term. But it's time to relearn JavaScript: it's seems like order guaranteed in the next ECMAScript version. – Felix Kling Apr 16 '15 at 08:24
  • I see, I misread the question. – joews Apr 16 '15 at 08:27
  • Sorry for the wrong terms. Edited the question. – cytsunny Apr 16 '15 at 08:30

1 Answers1

0

Underscore _.isEqual(object, other) Performs an optimized deep comparison between the two objects, to determine if they should be considered equal.

var stooge = {name: 'moe', luckyNumbers: [13, 27, 34]};
var clone  = {name: 'moe', luckyNumbers: [13, 27, 34]};
stooge == clone;
=> false
_.isEqual(stooge, clone);
=> true
Olexandr Petrov
  • 134
  • 1
  • 4
  • The OP only seems to care that the objects have the same property values. The don't have to be assigned to the same properties (at least that's how I read the question). – Felix Kling Apr 16 '15 at 08:11
  • I have tested. It seem that the comparison provided by you is not working when the order is changed. http://jsfiddle.net/7b1n4mjk/2/ – cytsunny Apr 16 '15 at 08:16