I am looking for an efficient way to remove all elements from a javascript array if they are present in another array.
// If I have this array:
var myArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
// and this one:
var toRemove = ['b', 'c', 'g'];
I want to operate on myArray to leave it in this state: ['a', 'd', 'e', 'f']
With jQuery, I'm using grep[]
and inArray[]
, which works well:
myArray = $.grep[myArray, function[value] {
return $.inArray[value, toRemove] < 0;
}];
Is there a pure javascript way to do this without looping and splicing?
Ivar
5,64212 gold badges51 silver badges57 bronze badges
asked Nov 13, 2013 at 15:13
6
Use the Array.filter[]
method:
myArray = myArray.filter[ function[ el ] {
return toRemove.indexOf[ el ] < 0;
} ];
Small improvement, as browser support for
Array.includes[]
has increased:
myArray = myArray.filter[ function[ el ] {
return !toRemove.includes[ el ];
} ];
Next adaptation using arrow functions:
myArray = myArray.filter[ [ el ] => !toRemove.includes[ el ] ];
answered Nov 13, 2013 at 15:16
SirkoSirko
70.6k19 gold badges143 silver badges175 bronze badges
16
ECMAScript 6 sets can permit faster computing of the elements of one array that aren't in the other:
const myArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
const toRemove = new Set[['b', 'c', 'g']];
const difference = myArray.filter[ x => !toRemove.has[x] ];
console.log[difference]; // ["a", "d", "e", "f"]
Since the lookup complexity for the V8 engine browsers use these days is O[1], the time complexity of the whole algorithm is O[n].
JohnK
6,4096 gold badges46 silver badges71 bronze badges
answered May 26, 2017 at 14:35
Benny NeugebauerBenny Neugebauer
47.8k23 gold badges225 silver badges193 bronze badges
2
var myArray = [
{name: 'deepak', place: 'bangalore'},
{name: 'chirag', place: 'bangalore'},
{name: 'alok', place: 'berhampur'},
{name: 'chandan', place: 'mumbai'}
];
var toRemove = [
{name: 'deepak', place: 'bangalore'},
{name: 'alok', place: 'berhampur'}
];
myArray = myArray.filter[ar => !toRemove.find[rm => [rm.name === ar.name && ar.place === rm.place] ]]
answered Apr 29, 2019 at 9:45
5
The filter
method should do the trick:
const myArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g'];
const toRemove = ['b', 'c', 'g'];
// ES5 syntax
const filteredArray = myArray.filter[function[x] {
return toRemove.indexOf[x] < 0;
}];
If your toRemove
array is large, this sort of lookup pattern can be inefficient. It would be more performant to create a map so that lookups are O[1]
rather than O[n]
.
const toRemoveMap = toRemove.reduce[
function[memo, item] {
memo[item] = memo[item] || true;
return memo;
},
{} // initialize an empty object
];
const filteredArray = myArray.filter[function [x] {
return toRemoveMap[x];
}];
// or, if you want to use ES6-style arrow syntax:
const toRemoveMap = toRemove.reduce[[memo, item] => [{
...memo,
[item]: true
}], {}];
const filteredArray = myArray.filter[x => toRemoveMap[x]];
Andriy
14.2k4 gold badges45 silver badges49 bronze badges
answered Nov 13, 2013 at 15:17
Ashwin BalamohanAshwin Balamohan
3,2522 gold badges24 silver badges47 bronze badges
1
If you are using an array of objects. Then the below code should do the magic, where an object property will be the criteria to remove duplicate items.
In the below example, duplicates have been removed comparing name of each item.
Try this example. //jsfiddle.net/deepak7641/zLj133rh/
var myArray = [
{name: 'deepak', place: 'bangalore'},
{name: 'chirag', place: 'bangalore'},
{name: 'alok', place: 'berhampur'},
{name: 'chandan', place: 'mumbai'}
];
var toRemove = [
{name: 'deepak', place: 'bangalore'},
{name: 'alok', place: 'berhampur'}
];
for[ var i=myArray.length - 1; i>=0; i--]{
for[ var j=0; j !toRemove.includes[item]];
console.log[myArray]
MH2K9
11.8k7 gold badges32 silver badges48 bronze badges
answered Aug 7, 2019 at 15:14
EggonEggon
1,7442 gold badges13 silver badges31 bronze badges
1
I just implemented as:
Array.prototype.exclude = function[list]{
return this.filter[function[el]{return list.indexOf[el]