I have two arrays:
var firstArr = [1,2,3,4,5];
var secondArr = [2,3];
How can I get an array like this:
[1,4,5]
I would like to use a solution that could be used when elements of the array are objects with multiple properties.
I have two arrays:
var firstArr = [1,2,3,4,5];
var secondArr = [2,3];
How can I get an array like this:
[1,4,5]
I would like to use a solution that could be used when elements of the array are objects with multiple properties.
Use filter
:
The
filter()
method creates a new array with all elements that pass the test implemented by the provided function.
For example:
firstArr.filter(function(item){
return secondArr.indexOf(item) === -1;
});
You can use this Function to remove items.
function removeA(arr) {
var what, a = arguments, L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax= arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
var ary = ['three', 'seven', 'eleven'];
removeA(ary, 'seven');