1

I have an array of objects.

var array = [obj1, obj2, obj3, objN]

every objects has 3 properties key, name, description.

How can i compare these objects for equality, two objects are equal if they have the same key.

But if a have lets say 4 objects they all must have the same key to be equal.

3
  • i thought taking the first object and comparing with the other objects but it doesnt feel right. Commented Oct 16, 2014 at 8:00
  • Seems like you have a good grasp of the concept, so what's the issue? Just write the code for the process you've outlined... Commented Oct 16, 2014 at 8:02
  • Comparing all objects to the first object is the way to go, actually. Commented Oct 16, 2014 at 8:05

2 Answers 2

7

You can do that using Array.prototype.every():

The every() method tests whether all elements in the array pass the test implemented by the provided function. (mdn)

Example:

var array = [obj1, obj2, obj3, objN];
var allTheSame = array.every(function(element){
    return element.key === array[0].key;
});

Please note that Array.prototype.every() is IE 9+. However, there's a nice polyfill on the mdn page for older versions of IE.

If you really want to use a for loop, you could do it like this:

var array = [obj1, obj2, obj3, objN];
var allTheSame = array.length == 1;

for(var i = 1; i < array.length && (array.length > 1 && allTheSame); i++){
    allTheSame = array[0].key == array[i].key;
}

Try it for:

[{key:1},{key:1},{key:1}]; // true
[{key:1},{key:1},{key:2}]; // false
[{key:1},{key:2},{key:1}]; // false
[{key:1}]; // true
Sign up to request clarification or add additional context in comments.

1 Comment

"But if a have lets say 4 objects they all must have the same key to be equal." I think the question is pretty clear, @Zeshan.
-3
var array = [obj1, obj2, obj3, objN];
for(i = 0, i < array.length; i++){
    for(j = i + 1; j < array.length; j++){
        if(array[i].key == array[j].key){
            // Your logic whatever you want to do here.
        }
    }
}

8 Comments

Why are you nesting for loops? o.O
@Cerbrus So that i can compare selecting index with rest of objects in array. In this way i dont have to go back to first index every time i go to next index. It works. Copy code and check in you console. you just have to pass actual objects in array
That's a lot of redundant checking. If you compare the first object to all other objects in the array, and they're all the same as the first, why would you need to do the same check for the 2nd object again?
@Cerbrus I'm avoiding issue you mentioned by second for loop so that i dont have to compare twice. Just give it a dry run.
There is no issue. If all elements in the array have to be the same, you only need to iterate over the array once, to compare all objects to the first one.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.