Lodash – filter method

  • Post author:
  • Post category:Lodash
  • Post comments:2 Comments
Lodash - filter method

Syntax Of Lodash filter method

_.filter(collection, [predicate=_.identity])

The Lodash filter method Iterates over elements of the collection, returning an array of all elements predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).

Arguments

  • collection (Array|Object) − The collection to iterate over.
  • [predicate=_.identity] (Function) − The function invoked per iteration.

Output

  • (Array) − Returns the new filtered array.

Example

var _ = require('lodash');
var list = [
   { 'user': 'Joe', 'age': 36, 'active': true },
   { 'user': 'Jake', 'age': 40, 'active': false }
];
 
var result = _.filter(list, function(user) { return user.active; });
console.log(result);

Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

Output

[ { user: 'Joe', age: 36, active: true } ]

Next Topic – Click Here

This Post Has 2 Comments

Leave a Reply