javascript js数组中的IndexOf元素使用使用下划线或jquery的真值函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7969031/
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
IndexOf element in js array using a truth function using underscore or jquery
提问by nakhli
I need basically the same functionality as Underscore's findbut with the index of the element as a result (and not the element itself).
我需要与 Underscore 的find基本相同的功能,但结果是元素的索引(而不是元素本身)。
As far as I know, Underscore's indexOflooks for a value and doesn't take a function. Same problem for jQuery's inArrayfunction.
据我所知,Underscore 的indexOf寻找一个值而不是一个函数。jQuery 的inArray函数也有同样的问题。
I came up with the following implementation, but I'm not sure it is the most efficient:
我想出了以下实现,但我不确定它是最有效的:
function myIndexOf(arr, filter) {
var index;
$.each(arr, function (i, elt) { if (filter(elt)) { index=i; return false; } });
return index;
}
采纳答案by Luc125
Here is a simple implementation:
这是一个简单的实现:
function find(collection, filter) {
for (var i = 0; i < collection.length; i++) {
if(filter(collection[i], i, collection)) return i;
}
return -1;
}
It will work on anyindexable object that has a length property, and you can pass a complex filter function of the form filter(element, index, collection)
(optional parameters).
它适用于任何具有长度属性的可索引对象,并且您可以传递表单的复杂过滤器函数filter(element, index, collection)
(可选参数)。
回答by Paolo Moretti
_.findIndex
is available in Lo-Dashand Underscore.js:
_.findIndex
在Lo-Dash和Underscore.js 中可用:
var characters = [
{ 'name': 'barney', 'age': 36, 'blocked': false },
{ 'name': 'fred', 'age': 40, 'blocked': true },
{ 'name': 'pebbles', 'age': 1, 'blocked': false }
];
_.findIndex(characters, function(chr) {
return chr.age < 20;
});
// → 2
_.findIndex(characters, { 'age': 36 });
// → 0
_.findIndex(characters, { 'name': 'dino' });
// → -1 (not found)
回答by Brian Nickel
Underscore uses the following implementation:
下划线使用以下实现:
_.find = function(obj, iterator, context) {
var result;
_.any(obj, function(value, index, list) {
if(iterator.call(context, value, index, list)) {
result = value;
return true;
}
});
return result;
}
This in turn calls, _.any
, which calls _.each
, which calls Array.prototype.forEach
. Efficiency isn't exactly the name of the game. It is more about utility.
这依次调用 ,_.any
调用_.each
,调用Array.prototype.forEach
。效率并不完全是游戏的名称。它更多地是关于效用的。
If you know for a fact that you are dealing with an array or array-like object, you can use @Thor84no's solution and simply loop through the array until your filter condition is met. If not, and you may be dealing with objects as well, I would simply rewrite _.find as _.findIndex and use result = index;
.
如果您知道您正在处理数组或类似数组的对象,则可以使用@Thor84no 的解决方案并简单地遍历数组,直到满足过滤条件。如果没有,并且您可能也在处理对象,我会简单地将 _.find 重写为 _.findIndex 并使用result = index;
.
回答by Joel
You can just use a local variable to get that like so:
您可以只使用局部变量来获得这样的结果:
var idx = 0;
var match = _.detect(my_list, function(itm){
return itm.something == some_test_value || ++idx == my_list.length && (idx = -1);
}
Viola. You got the index AND the matching value. If the iterator reaches the end, the index goes to -1, and if you hit a match, it short-circuits on that index.
中提琴。你得到了索引和匹配值。如果迭代器到达末尾,则索引变为 -1,如果匹配,则在该索引上短路。
回答by Thor84no
It would be more efficient to do the for loop yourself and use break;
(or return
) to leave the loop once you've found the index.
自己执行 for 循环并在找到索引后使用break;
(或return
) 离开循环会更有效。
for (var i = 0; i < arr.length; i++) {
if (filter(arr[i])) {
return i;
}
}