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.

Community
  • 1
  • 1
w2olves
  • 2,229
  • 10
  • 33
  • 60
  • 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. – Patrick Roberts Jul 01 '16 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 – juvian Jul 01 '16 at 19:42

5 Answers5

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.

Lifz
  • 690
  • 6
  • 10
  • 3
    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 ? – AlexG Jul 01 '16 at 19:41
  • 1
    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. – w2olves Jul 01 '16 at 19:43
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])
)
AlexG
  • 3,617
  • 1
  • 23
  • 19
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;
};
Redu
  • 25,060
  • 6
  • 56
  • 76
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));
ryeballar
  • 29,658
  • 10
  • 65
  • 74
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

Sergio Ivanuzzo
  • 1,820
  • 4
  • 29
  • 59