I make an ajax call that returns an array of objects. The array can contain 1 object or 20, it is not known before making the call.
The objects will have the same properties but different values. I can compare two objects like this:
function compareObjects(x, y) {
var objectsAreSame = true;
for(var propertyName in x) {
if(x[propertyName] !== y[propertyName]) {
objectsAreSame = false;
break;
}
}
return objectsAreSame;
}
How can I extend this to compare several objects or is there a better way to compare ?
Edit#1 Similar question here: Object comparison in JavaScript
However, I am not trying to compare two objects, I am trying to find the shortest possible way to compare an unknown array of objects. I apologize for any confusion.
Edit #2 I am not asking how to compare two objects. I am asking how to take an array of unknown quantities and compare every object inside and check if every object is the same.
Like arrayofObjects[obj1, obj2...objn] simplest way to say obj1, through objn are exactly the same.