javascript 获取与迭代器函数匹配的集合的第一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19497493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get first element of a collection that matches iterator function
提问by ducin
I would like to achieve something like _.first
with _.filter
, that is, having a collection of elements, I'd like to get the first one (if exists) that matches a truth test (iterator).
我想实现类似_.first
with 的东西_.filter
,也就是说,拥有一组元素,我想获得第一个(如果存在)与真值测试(迭代器)匹配的元素。
For example, given an array like the following:
例如,给定一个如下所示的数组:
var arr = [{a: 1}, {a: 5}, {a: 9}, {a: 11}, {a: 15}]
I would like to getthe first (and only first) element that matches my custom function:
我想获取与我的自定义函数匹配的第一个(也是唯一的第一个)元素:
_.filterFirst(arr, function(el) { return el.a > 10; }); // make it
So far:
迄今为止:
_.first(arr) == {a:1}
_.filter(arr, function(...)) == [{a:11}, {a:15}]
Is there a clean solution to do this which is better than _.first(_.filter(arr, iterator))
?
有没有比这更好的干净解决方案_.first(_.filter(arr, iterator))
?
回答by Tahir Hassan
You can use find
:
您可以使用find
:
Looks through each value in the list, returning the first one that passes a truth test (iterator), or undefined if no value passes the test. The function returns as soon as it finds an acceptable element, and doesn't traverse the entire list.
查看列表中的每个值,返回第一个通过真值测试(迭代器)的值,如果没有值通过测试,则返回 undefined。该函数在找到可接受的元素后立即返回,并且不会遍历整个列表。
Using your example:
使用您的示例:
var g = _.find(arr, function (x) { return x.a > 10 })
See the main page: http://underscorejs.org
查看主页:http: //underscorejs.org
Another thing to note (which might be your question) is the chain
function to join calls together:
另一件需要注意的事情(这可能是你的问题)是将chain
调用连接在一起的函数:
var g = _.chain(arr).filter(function (x) { return x.a > 10 }).first().value()
Notice the calls to filter
and `first' which can follow each other without any nesting.
请注意对filter
和“first”的调用,它们可以相互跟随而无需任何嵌套。
回答by Juan Cortines
"_.find" is a good solution.
“_.find”是一个很好的解决方案。
An alternative solution, maybe faster, is to use "Array.prototype.every" in this way:
另一种可能更快的替代解决方案是以这种方式使用“Array.prototype.every”:
var match;
arr.every(function(x) { if (x.a > 10) { match = x; return false;} return true; })
回答by BigMan73
_.find - in lodash: https://lodash.com/docs/4.17.10#find
_.find - 在 lodash 中:https://lodash.com/docs/4.17.10#find
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false },
{ 'user': 'pebbles', 'age': 1, 'active': true }
];
_.find(users, function(o) { return o.age < 40; });
// => object for 'barney'
// The `_.matches` iteratee shorthand.
_.find(users, { 'age': 1, 'active': true });
// => object for 'pebbles'
// The `_.matchesProperty` iteratee shorthand.
_.find(users, ['active', false]);
// => object for 'fred'
// The `_.property` iteratee shorthand.
_.find(users, 'active');
// => object for 'barney'