I inherited a monster mess of an AngularJS project at my new job. I've been trying to resolve a bug recently that has the following Lodash statement:
var group = _.find(groupList, {id: id});
From the documentation available on https://lodash.com/docs/3.10.1#find , which is the closest I can find to version 3.7.0, the _.find function iterates over a collection and returns the first object that the statement is "truthy" for. The second item inside the expression would represent a function to be executed.
However, I am confused about what the {id: id} is doing in this instance. Is this an Angular expression? What is it doing here exactly? Any help would be much appreciated.
It means - search for an object in the array, that has the property id
, with the value of the variable id
.
var groupList = [{ id: 21, name: 'cats' }, { id: 17, name: 'dogs' }, { id: 701, name: 'rats' }]; var id = 17; var group = _.find(groupList, {id: id}); console.log(group);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Maybe try reading all the documentation in the link you posted? Specifically look at the Examples:
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false },
{ 'user': 'pebbles', 'age': 1, 'active': true }
];
_.result(_.find(users, function(chr) {
return chr.age < 40;
}), 'user');
// => 'barney'
// using the `_.matches` callback shorthand
_.result(_.find(users, { 'age': 1, 'active': true }), 'user');
// => 'pebbles'
// using the `_.matchesProperty` callback shorthand
_.result(_.find(users, 'active', false), 'user');
// => 'fred'
// using the `_.property` callback shorthand
_.result(_.find(users, 'active'), 'user');
// => 'barney'
I believe the 2nd example that says
// using the
_.matches
callback shorthand
answers your question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.