2

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.

2
  • While you may consider it a "similar" question, it is in fact an identical question. Arrays are Objects in JavaScript, and as such, the answers provided in that link also answer your question. Commented Jul 1, 2016 at 19:42
  • Could you make a simple example of input and output? Not sure if you want to compare 2 different array of objects or the objects within Commented Jul 1, 2016 at 19:42

5 Answers 5

2

Stringify 'em!

var same = JSON.stringify(x) === JSON.stringify(y);

Edit: OP's slight edit can change this answer a bit. I thought he wanted to know if obj1 === obj2 (if they're exactly the same). This will do that well but not if properties can be in different orders and still be considered "same" for his use case.

Sign up to request clarification or add additional context in comments.

2 Comments

This is very fragile and dependent on the implementation of JSON.stringify being and staying stable. Why should the keys be in the same order across all browsers and across all calls ?
This solution would would work in a non ajax situation I believe, my array has same properties but the order is not always the same.
1

You can use Array.prototype.every() and compare all objects in the array using the first object as reference for the comparison.

function compareArray(array) {
  return array.every(compareObjects.bind(null, array[0]));
}

function compareObjects(x, y) {
   var objectsAreSame = true;
   for(var propertyName in x) {
      if(x[propertyName] !== y[propertyName]) {
         objectsAreSame = false;
         break;
      }
   }
   return objectsAreSame;
}

function compareArray(array) {
  return array.every(compareObjects.bind(null, array[0]));
}

var array1 = [ { a: 1 }, { a: 1 }, { a: 1 }, { a: 1 } ];
var array2 = [ { a: 1 }, { b: 1 }, { c: 1 }, { d: 1 } ];

console.log(compareArray(array1));
console.log(compareArray(array2));

Comments

1

Keeping your fonction as is (it's very simplistic but let's assume it works in your use case):

const sameArray = (newArray.length === oldArray.length) && newArray.every((item, index) =>
  compareObjects(item, oldArray[index])
)

Comments

1

I would do this with a generic method Object.prototype.compare() as follows

Object.prototype.compare = function(o){
  var ok = Object.keys(this);
  return typeof o === "object" && ok.length === Object.keys(o).length ? ok.every(k => this[k] === o[k]) : false;
};

Comments

0

In case if you need to compare array of objects and objects can contain arrays and each array can contain something different from object, you can use this:

const isEqual = (a, b) => {
    if (typeof a === "object" && typeof b === "object") {
        if (Array.isArray(a) && Array.isArray(b)) {
            return a.every((item, index) => isEqual(item, b[index]));
        }
        return Object.entries(a).toString() === Object.entries(b).toString();
    }

    return a === b;
};

For array this function will called recursively

Comments

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.