Skip to content

filter

Summary

Returns a new list with only the elements that passed a predicate.

Tip

This function is curried.

Tip

Act as a transducer when composed with drop, take or map.

Description

Keeps the elements in xs that satisfied the predicate fn and returns a new list of the same type. The predicate must return logical true.

Examples

Works with arrays and objects

1
2
3
4
5
6
7
const even = filter(x => x % 2 === 0);

even([1, 2, 3, 4]);
//=> [2, 3]

even({a: 1, b: 2, c: 3, d: 4});
//=> {b: 2, d: 4}

Parameters

Name Type Description
pred function
xs Array or Object

Return

Array or Object