Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inverse of [].filter in JS?

I realize that I can do:

arr = arr.filter(function(n){ return !filterFunc(n); }); 

But is there any way to just invert a filter without wrapping the filterer in an anon function?

It just seems cumbersome.

like image 894
user2958725 Avatar asked Nov 22 '13 15:11

user2958725


People also ask

What is the opposite of filter in JS?

Lodash provides a reject function that does the exact opposite of filter.

Does filter alter array JavaScript?

JavaScript Array filter() The filter() method creates a new array filled with elements that pass a test provided by a function. The filter() method does not execute the function for empty elements. The filter() method does not change the original array.

Can you use filter on an array of objects JavaScript?

One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.

Can you filter an array?

Using filter() on an Array of Numbers The item argument is a reference to the current element in the array as filter() checks it against the condition . This is useful for accessing properties, in the case of objects. If the current item passes the condition , it gets returned to the new array.


1 Answers

You can use an arrow function:

const a = someArr.filter(someFilter); const a = someArr.filter(e => !someFilter(e)); 
like image 164
apscience Avatar answered Oct 09 '22 06:10

apscience