Reputation: 11030
What is the correct way of removing objects that have a certain field in JavaScript?
I can include fields with the following:
filteredResult = filteredResult.filter(e => e.selectedFields.includes("Red"));
But if I wanted to remove all properties that have this field what would I do? There doesn't seem to be a "Remove" from the documentation.
Upvotes: 48
Views: 120658
Reputation: 4001
You can negate the includes
result or even cleaner just by using the indexOf
property:
const arr = [1, 2, 3];
const notIncludes1 = (arr, val) => !(arr.includes(0));
const notIncludes2 = (arr, val) => arr.indexOf(val) === -1;
Upvotes: 4
Reputation: 183
filteredResult = filteredResult.filter((l) => (['Red', 'Blue','Yellow'].every(y => !l.selectedFields.toLowerCase().includes(y.toLowerCase()))));
Upvotes: -1
Reputation: 10069
Just negate it.
filteredResult = filteredResult.filter(e => !e.selectedFields.includes("Red"))
Upvotes: 98