Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 346
lost9123193 Avatar asked Feb 03 '18 00:02

lost9123193


People also ask

What is the alternative for includes in JavaScript?

indexOf() The Array#indexOf() function is a common alternative to includes() . The indexOf() function returns the first index in the array at which it found valueToFind , or -1 otherwise.

Does not include in array JavaScript?

To check if an array doesn't include a value, use the logical NOT (!) operator to negate the call to the includes() method. The NOT (!) operator returns false when called on a true value and vice versa.

How do you check if an array includes an object in JavaScript?

Using includes() Method: If array contains an object/element can be determined by using includes() method. This method returns true if the array contains the object/element else return false. Example: html.

What does .includes do in JavaScript?

In JavaScript, includes() method determines whether a string contains the given characters within it or not. This method returns true if the string contains the characters, otherwise, it returns false.


1 Answers

Just negate it.

filteredResult = filteredResult.filter(e => !e.selectedFields.includes("Red")) 
like image 74
tremby Avatar answered Oct 16 '22 23:10

tremby