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.
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.
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.
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.
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.
Just negate it.
filteredResult = filteredResult.filter(e => !e.selectedFields.includes("Red"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With