Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check the difference between two arrays of objects in javascript [duplicate]

I need some help. How can I get the array of the difference on this scenario:

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' }
];

I want the array of difference:

// [{ id: 2, name: 'pablo' }, { id: 3, name: 'escobar' }]

How is the most optimized approach?

I´m trying to filter a reduced array.. something on this line:

var Bfiltered = b1.filter(function (x) {
return x.name !== b2.reduce(function (acc, document, index) {
    return (document.name === x.name) ? document.name : false
},0)

});

console.log("Bfiltered", Bfiltered);
// returns { id: 0, name: 'john' }, { id: 2, name: 'pablo' }, { id: 3, name: 'escobar' } ]

Thanks,

Robot

like image 781
RobotMan Avatar asked Jun 01 '18 17:06

RobotMan


People also ask

How will you differentiate between arrays and objects in JavaScript?

Both objects and arrays are considered “special” in JavaScript. Objects represent a special data type that is mutable and can be used to store a collection of data (rather than just a single value). Arrays are a special type of variable that is also mutable and can also be used to store a list of values.

How do you find duplicate objects in an array?

Using the indexOf() method In this method, what we do is that we compare the index of all the items of an array with the index of the first time that number occurs. If they don't match, that implies that the element is a duplicate. All such elements are returned in a separate array using the filter() method.


2 Answers

.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);
like image 185
Chiller Avatar answered Sep 20 '22 14:09

Chiller


You can use filter to filter/loop thru the array and some to check if id exist on array 2

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 result = b1.filter(o => !b2.some(v => v.id === o.id));

console.log(result);

Above example will work if array 1 is longer. If you dont know which one is longer you can use sort to arrange the array and use reduce and filter.

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 result = [b1, b2].sort((a,b)=> b.length - a.length)
                     .reduce((a,b)=>a.filter(o => !b.some(v => v.id === o.id)));

console.log(result);
like image 29
Eddie Avatar answered Sep 20 '22 14:09

Eddie