获取与迭代器函数匹配的集合的第一个元素

时间:2022-07-13 19:31:41

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).

我希望用_.filter实现像_.first这样的东西,也就是说,有一个元素集合,我想获得与真值测试(迭代器)匹配的第一个(如果存在)。

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))更好?

3 个解决方案

#1


59  

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:

另一件需要注意的事情(可能是你的问题)是连接调用的链函数:

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'的调用,它们可以相互跟随而不需要任何嵌套。

#2


0  

"_.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; })

#3


0  

_.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'

#1


59  

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:

另一件需要注意的事情(可能是你的问题)是连接调用的链函数:

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'的调用,它们可以相互跟随而不需要任何嵌套。

#2


0  

"_.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; })

#3


0  

_.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'