js 数组中的 IndexOf 元素使用下划线或 jquery 使用真值函数

发布于 2024-12-13 03:51:43 字数 520 浏览 1 评论 0原文

我需要与 Underscore 的 find 基本相同的功能,但结果是元素的索引(而不是元素本身) )。

据我所知,Underscore 的 indexOf 寻找一个值并且不接受函数。 jQuery 的 inArray 函数也存在同样的问题。

我想出了以下实现,但我不确定它是最有效的:

function myIndexOf(arr, filter) {
  var index;
  $.each(arr, function (i, elt) { if (filter(elt)) { index=i; return false; } });
  return index;
}

I need basically the same functionality as Underscore's find but with the index of the element as a result (and not the element itself).

As far as I know, Underscore's indexOf looks for a value and doesn't take a function. Same problem for jQuery's inArray function.

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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北凤男飞 2024-12-20 03:51:43

_.findIndex 可在 Lo-DashUnderscore.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)

_.findIndex is available in Lo-Dash and 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)
花期渐远 2024-12-20 03:51:43

这是一个简单的实现:

function find(collection, filter) {
    for (var i = 0; i < collection.length; i++) {
        if(filter(collection[i], i, collection)) return i;
    }
    return -1;
}

它将适用于具有 length 属性的任何可索引对象,并且您可以传递 filter(element, index, collection) 形式的复杂过滤器函数代码>(可选参数)。

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 any indexable object that has a length property, and you can pass a complex filter function of the form filter(element, index, collection) (optional parameters).

安稳善良 2024-12-20 03:51:43

Underscore 使用以下实现:

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

它依次调用 _.any,后者又调用 _.each,后者又调用 Array.prototype.forEach。效率并不完全是游戏的名称。它更多的是关于实用性。

如果您知道您正在处理数组或类似数组的对象,则可以使用@Thor84no 的解决方案并简单地循环遍历数组,直到满足过滤条件。如果没有,并且您可能也在处理对象,我只需将 _.find 重写为 _.findIndex 并使用 result = index;

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.

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

皓月长歌 2024-12-20 03:51:43

您可以使用局部变量来获取它,如下所示:

var idx = 0; 
var match = _.detect(my_list, function(itm){
    return itm.something == some_test_value || ++idx == my_list.length && (idx = -1);
}

Viola。您获得了索引和匹配值。如果迭代器到达末尾,索引将变为 -1,如果命中匹配项,它将在该索引上短路。

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.

稳稳的幸福 2024-12-20 03:51:43

自己执行 for 循环并在找到索引后使用 break; (或 return)离开循环会更有效。

for (var i = 0; i < arr.length; i++) {
    if (filter(arr[i])) {
        return i;
    }
}

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 (var i = 0; i < arr.length; i++) {
    if (filter(arr[i])) {
        return i;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文