How to Get the Difference Between Two Arrays of Objects in JavaScript

check the difference between two arrays of objects in javascript

.Filter() and .some() functions will do the trick

var b1 = [  { id: 0, name: 'john' },   { id: 1, name: 'mary' },   { id: 2, name: 'pablo' },   { id: 3, name: 'escobar' } ]; 
var b2 = [ { id: 0, name: 'john' }, { id: 1, name: 'mary' }];
var res = b1.filter(item1 => !b2.some(item2 => (item2.id === item1.id && item2.name === item1.name)))
console.log(res);

Getting difference between 2 arrays of objects in JavaScript

You were almost there, use id instead of x

existingBillPlans.filter(item => !billPlans.some(other => item.id == other.id));

Demo

var existingBillPlans = [{    id: 2,    fees: 10000,    name: 'Yearly Plan',    cycle: 12  },  {    id: 3,    fees: 1500,    name: 'Two Months Plan',    cycle: 2  },  {    id: 4,    fees: 2500,    name: 'Three Months Plan',    cycle: 3  },  {    id: 5,    fees: 3000,    name: 'Four Months Plan',    cycle: 4  },  {    id: 181,    fees: 4000,    name: 'Five Months Plan',    cycle: 5  },  {    id: 182,    fees: 5000,    name: 'Six Months Plan',    cycle: 6  },  {    id: 183,    fees: 6000,    name: 'Seven Months Plan',    cycle: 7  }]var billPlans = [{    id: 2,    fees: 10000,    name: 'Yearly Plan',    cycle: 12  },  {    id: 3,    fees: 1500,    name: 'Two Months Plan',    cycle: 2  },  {    id: 4,    fees: 2500,    name: 'Three Months Plan',    cycle: 3  }]var output = existingBillPlans.filter(item => !billPlans.some(other => item.id == other.id));
console.log(output);

Comparing two arrays of objects, and exclude the elements who match values into new array in JS

Just using the Array iteration methods built into JS is fine for this:

var result1 = [    {id:1, name:'Sandra', type:'user', username:'sandra'},    {id:2, name:'John', type:'admin', username:'johnny2'},    {id:3, name:'Peter', type:'user', username:'pete'},    {id:4, name:'Bobby', type:'user', username:'be_bob'}];
var result2 = [ {id:2, name:'John', email:'johnny@example.com'}, {id:4, name:'Bobby', email:'bobby@example.com'}];
var props = ['id', 'name'];
var result = result1.filter(function(o1){ // filter out (!) items in result2 return !result2.some(function(o2){ return o1.id === o2.id; // assumes unique id });}).map(function(o){ // use reduce to make objects with only the required properties // and map to apply this to the filtered array as a whole return props.reduce(function(newo, name){ newo[name] = o[name]; return newo; }, {});});
document.body.innerHTML = '<pre>' + JSON.stringify(result, null, 4) + '</pre>';

Difference of two arrays of objects with lodash

You can use xorBy to indicate a property used for comparison:

const A1 = [{id: 1, nome: "Ruan"}, {id: 2, nome: "Gleison"}]
const A2 = [{id: 2, nome: "Gleison"}, {id: 3, nome: "Geraldo"}]

const results = _.xorBy(A1, A2, 'id'); // or 'nome'

console.log(results)
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js"></script>

Comparing Arrays of Objects in JavaScript

EDIT: You cannot overload operators in current, common browser-based implementations of JavaScript interpreters.

To answer the original question, one way you could do this, and mind you, this is a bit of a hack, simply serialize the two arrays to JSON and then compare the two JSON strings. That would simply tell you if the arrays are different, obviously you could do this to each of the objects within the arrays as well to see which ones were different.

Another option is to use a library which has some nice facilities for comparing objects - I use and recommend MochiKit.


EDIT: The answer kamens gave deserves consideration as well, since a single function to compare two given objects would be much smaller than any library to do what I suggest (although my suggestion would certainly work well enough).

Here is a naïve implemenation that may do just enough for you - be aware that there are potential problems with this implementation:

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

The assumption is that both objects have the same exact list of properties.

Oh, and it is probably obvious that, for better or worse, I belong to the only-one-return-point camp. :)



Related Topics



Leave a reply



Submit