Lodash教程

Lodash find method

语法:

_.find(collection, [predicate=_.identity], [fromIndex=0])
Iterates over elements of collection, returning the first element predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).

参数:

collection (Array|Object) − The collection to inspect. [predicate=_.identity] (Function) − The function invoked per iteration. [fromIndex=0] (number) − The index to search from.

输出:

(*) − Returns the matched element, else undefined.

例子:

var _ = require('lodash');
var list = [
   { 'user': 'Joe', 'age': 36, 'active': true },
   { 'user': 'Jake', 'age': 40, 'active': false }
];
 
var result = _.find(list, function(user) { return user.active; });
console.log(result);
Save the above program in tester.js. Run the following command to execute this program.

命令:

\>node tester.js

输出:

{ user: 'Joe', age: 36, active: true }
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4