lost9123193
lost9123193

Reputation: 11030

Javascript Arrays Opposite of Includes

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

Answers (3)

Constantin
Constantin

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

Naim R.
Naim R.

Reputation: 183

filteredResult = filteredResult.filter((l) => (['Red', 'Blue','Yellow'].every(y => !l.selectedFields.toLowerCase().includes(y.toLowerCase()))));

Upvotes: -1

tremby
tremby

Reputation: 10069

Just negate it.

filteredResult = filteredResult.filter(e => !e.selectedFields.includes("Red"))

Upvotes: 98

Related Questions