remove items from array that are in other array code example
Example 1: remove the items in array which are present in another javascript
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) ))
Example 2: remove an element in array useing another array
Remove an item comparing from other array
const toRemoveMap = toRemove.reduce(
function(memo, item) {
memo[item] = memo[item] || true;
return memo;
},
{}
);
const filteredArray = myArray.filter(function (x) {
return toRemoveMap[x];
});
const toRemoveMap = toRemove.reduce((memo, item) => ({
...memo,
[item]: true
}), {});
const filteredArray = myArray.filter(x => toRemoveMap[x.id]);
From <https://stackoverflow.com/questions/19957348/remove-all-elements-contained-in-another-array>