Many of the filtering operations that you want to perform are simple ones. These include simple equality checks, greater than checks, and less than checks. Each of these comparisons is executed in a callback function that's passed to the filter()
method.
Strict equality is used when you're looking for a value in a collection, and you have something with which you can compare it. For example, you could define a filter
function that uses strict equality to look for values that equal 1
or 2
, as follows:
const filter = i => i === 1 || i === 2;
You can then pass this function to the filter()
method of a list:
const myList = List.of(1, 2, 3); const myFilteredList = myList.filter(filter); console.log('myList', myList.toJS()); // -> myList [ 1, 2, 3 ] console.log('myFilteredList', myFilteredList.toJS()); // -> myFilteredList [ 1, 2 ]
You can use the same function to filter maps, as shown here:
const myMap = Map.of( 'one', 1, 'two', 2, ...