Remove all elements from an array that already exist in another

3

I have two arrays with objects in javascript:

var a = [{objeto1}, {objeto2}, {objeto3}, {objeto4}, {objeto5}];
var b = [{objeto1}, {objeto2}];

How can I do what is in the array b exit the array a ?

    
asked by anonymous 18.05.2017 / 19:56

5 answers

5

You can do following.:

var objeto1 = { value: 'A' };
var objeto2 = { value: 'B' };
var objeto3 = { value: 'C' };
var objeto4 = { value: 'D' };
var objeto5 = { value: 'E' };

var listaA = [objeto1, objeto2, objeto3, objeto4, objeto5];
var listaB = [objeto1, objeto2];

listaA = listaA.filter(function (objeto) { 
  return listaB.indexOf(objeto) == -1 
});
console.log(listaA);

But remember that object comparison is done by reference, so even though both objects have exactly the same properties and values, they will still be considered as different.

var objeto1 = { value: 'A' };
var objeto2 = Object.assign({}, objeto1);
console.log(objeto1, objeto2, objeto1 == objeto2);

Optionally, you can compare the objects using the hash of the same.

Array.prototype.except = function(lista) {
  lista = lista.map(function (object) {
    return objectHash.sha1(object);
  });
  return this.filter(function (objeto) { 
    var objHash = objectHash.sha1(objeto)
    return lista.indexOf(objHash) == -1;
  });
}

var listaA = [
  { value: 'A' }, 
  { value: 'B' }, 
  { value: 'C' }, 
  { value: 'D' }, 
  { value: 'E' }
];
var listaB = [
  { value: 'B' }, 
  { value: 'D' }
];
var lista = listaA.except(listaB);
console.log(lista);
<script src="https://rawgit.com/puleos/object-hash/master/dist/object_hash.js"></script>
    
18.05.2017 / 20:06
2

Another option is to remove the index from the array itself:

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

a.map(function(item, index){
  JSON.stringify(b).indexOf(JSON.stringify(item)) == -1 ? '' : a.splice(index,1) ;
});

console.log(a);
    
18.05.2017 / 20:14
1

If you want to remove all elements contained in array a from array b , you can use the filter :

var c = a.filter(function(item) {
  return b.indexOf(item) === -1; // Não foi encontrado
});

Returned elements will be those that get a result true , that is, they are not contained in array b .

    
18.05.2017 / 20:04
1

You can use the native Javascript function splice .

  

The splice () method changes the contents of a list by adding new elements while removing old elements.

The function receives up to three parameters, but we only need the first two. The first is the index where you are going to remove something, and the second parameter is the amount of items you want to remove from that index.

Logo:

var objeto1 = {}, objeto2 = {}, objeto3 = {};
var a = [objeto1, objeto2, objeto3];
var b = [objeto2];

for (var i = a.length - 1; i >= 0; i++) {
    if (b.indexOf(a[i]) > -1) {
        a = a.splice(i, 1);
    }
}

At the end of the above code, the a vector will contain only the objeto1 and objeto3 objects.

Note that the comparison between objects in Javascript, used internally by the% method of% of the vector, is by reference. This type of comparison works if you add the same objects to the two vectors. If you add separate objects with similar properties, you will still need to implement a function to fetch the objects.

    
18.05.2017 / 20:16
1

Only make a filter and check with includes if each item in the a array exists in the b array.

Just remember that object comparison is always done by reference, ie even if the properties of two objects have the same values they are not the same.

var obj1 = { value: 'A' };
var obj2 = { value: 'B' };
var obj3 = { value: 'C' };
var obj4 = { value: 'E' };

var a = [ obj1, obj2, obj3, obj4];
var b = [ obj1, obj2 ];

a = a.filter(function(el) {
  return !b.includes(el);
} );

console.log(a);
    
18.05.2017 / 20:05