I want to delete elements from array in array (can't remember how it is named in math), for example:
var arr1 = ['uno', 'dos', 'tres', 'cuatro'],
arr2 = ['dos', 'cuatro'],
arr3 = arr1.without(arr2);
//arr3 === ['uno', 'tres']
Greetings
I want to delete elements from array in array (can't remember how it is named in math), for example:
var arr1 = ['uno', 'dos', 'tres', 'cuatro'],
arr2 = ['dos', 'cuatro'],
arr3 = arr1.without(arr2);
//arr3 === ['uno', 'tres']
Greetings
Array#filter
may be helpful.
var arr1 = ['uno', 'dos', 'tres', 'cuatro'],
arr2 = ['dos', 'cuatro'],
arr3 = arr1.filter(v => arr2.indexOf(v) == -1);
console.log(arr3);
var arr1 = ['uno', 'dos', 'tres', 'cuatro'];
var arr2 = ['dos', 'cuatro'];
arr1 = arr1.filter(function(item){
return arr2.indexOf(item) < 0;
});
console.log(arr1)
You could use Array#includes
and take the negated result.
var arr1 = ['uno', 'dos', 'tres', 'cuatro'],
arr2 = ['dos', 'cuatro'],
arr3 = arr1.filter(v => !arr2.includes(v));
console.log(arr3);
Here you are, a ready function for that:
function substractArrays (arr1, arr2) {
if (arr2.length) {
return arr1.filter(item => {
return !arr2.some(elem => {
return item.id === elem.id;
});
});
} else {
return arr1;
}
}