peter
peter

Reputation: 2103

Get the difference between two arrays of objects

I've got two arrays of objects, the difference between them is only that arrayAfter will have an element added:

var arrayBefore = [
  {"name":"Alan","height":"171","weight":"66"},
  {"name":"Ben","height":"182","weight":"90"}
 ];

var arrayAfter= [
  {"name":"Alan","height":"171","weight":"66"},
  {"name":"Ben","height":"182","weight":"90"},
  {"name":"Chris","height":"163","weight":"71"}
 ];

"name" is always unique!

How can I find out which one is the element that has been added? I've tried ending up using nested for loops, but this seems overcomplicated.

I've also found the this nice idea:

var diff = $(arrayAfter).not(arrayBefore ).get();

However, that does not seem to work on arrays of objects straight ahead.

Is there some easy way to get the difference?

Upvotes: 2

Views: 202

Answers (4)

Etheryte
Etheryte

Reputation: 25310

For a generic method you can combine Array.prototype.filter() with Array.prototype.reduce() which iterates over the object keys:

arrayAfter.filter(function(after) {
    return !arrayBefore.reduce(function(found, before) {
        if (!found) {
            found = true;
            for (key in before) {
                if (before.hasOwnProperty(key)) {
                    found = found && (before[key] === after[key]);
                }
            }
        }
        return found;
    }, false);
}); //[{name: "Chris", height: "163", weight: "71"}]

Upvotes: 1

UnknownFury
UnknownFury

Reputation: 314

I believe jQuery will have nothing that will directly solve your problem here. Your problem being comparing objects for equality.

I am assuming that name is unique. If not, for this method you will need a unique identifier for data. If you absolute do not have one then you could concat all data to get one.

// first make a map of objects in before
var before = {};
arrayBefore.forEach(function(o){
   before[o.name] = o;
});

// now we get the elements of after that do not exist in our hashmap
var result = arrayAfter.filter(function(o){
    return !(o.name in before);
});

You can obviously wrap this up in a general function.

Upvotes: 0

tymeJV
tymeJV

Reputation: 104775

If only the name indicates uniqueness, you can do:

//Get a list of all the names in the before array
var beforeNames = arrayBefore.map(function(person) { return person.name });

//Filter the after array to only contain names not contained in the before array
var uniqueObjects = arrayAfter.filter(function(person) {
    return beforeNames.indexOf(person.name) === -1;
});

console.log(uniqueObjects); //[{"name":"Chris","height":"163","weight":"71"}]

Demo: http://jsfiddle.net/tehgc8L5/

Upvotes: 2

Mike Cluck
Mike Cluck

Reputation: 32511

You can use Array.prototype.filter and filter out those elements in the previous array.

var differences = arrayAfter.filter(function(el) {
  return arrayBefore.indexOf(el) === -1;
});

Upvotes: 0

Related Questions