Lodash filter method
语法:
_.filter(collection, [predicate=_.identity])
Iterates over elements of collection, returning an array of all elements predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).
参数:
collection (Array|Object) − The collection to iterate over.
[predicate=_.identity] (Function) − The function invoked per iteration.
输出:
(Array) − Returns the new filtered array.
例子:
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.
命令:
输出:
[ { user: 'Joe', age: 36, active: true } ]