且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

查找具有下划线中特定键值的对象的数组索引

更新时间:2022-11-14 09:18:19

我不知道是否存在执行此操作的现有下划线方法,但您可以实现与普通javascript相同的结果。

I don't know if there is an existing underscore method that does this, but you can achieve the same result with plain javascript.

Array.prototype.getIndexBy = function (name, value) {
    for (var i = 0; i < this.length; i++) {
        if (this[i][name] == value) {
            return i;
        }
    }
    return -1;
}

然后你可以这样做:

var data = tv [tv.getIndexBy(id,2)]