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

Javascript 在下划线中查找具有特定键值的对象的数组索引,javascript,underscore.js,Javascript,Underscore.js,在下划线中,我可以成功找到具有特定键值的项 var tv = [{id:1},{id:2}] var voteID = 2; var data = _.find(tv, function(voteItem){ return voteItem.id == voteID; }); //data = { id: 2 } 但是我如何找到该对象出现在哪个数组索引中?我不知道是否有一个现有的下划线方法可以做到这一点,但是使用纯javascript可以获得相同的结果 Array.prototype.getI

在下划线中,我可以成功找到具有特定键值的项

var tv = [{id:1},{id:2}]
var voteID = 2;
var data = _.find(tv, function(voteItem){ return voteItem.id == voteID; });
//data = { id: 2 }

但是我如何找到该对象出现在哪个数组索引中?

我不知道是否有一个现有的下划线方法可以做到这一点,但是使用纯javascript可以获得相同的结果

Array.prototype.getIndexBy = function (name, value) {
    for (var i = 0; i < this.length; i++) {
        if (this[i][name] == value) {
            return i;
        }
    }
    return -1;
}
Array.prototype.getIndexBy=函数(名称、值){
for(var i=0;i
然后你可以做:

var data=tv[tv.getIndexBy(“id”,2)]
保持简单:

// Find the index of the first element in array
// meeting specified condition.
//
var findIndex = function(arr, cond) {
  var i, x;
  for (i in arr) {
    x = arr[i];
    if (cond(x)) return parseInt(i);
  }
};

var idIsTwo = function(x) { return x.id == 2 }
var tv = [ {id: 1}, {id: 2} ]
var i = findIndex(tv, idIsTwo) // 1
或者,对于非仇恨者,CoffeeScript变体:

findIndex = (arr, cond) ->
  for i, x of arr
    return parseInt(i) if cond(x)

如果您想保留下划线,以便谓词函数更灵活,下面有两个想法

方法1 由于
.find
的谓词同时接收元素的值和索引,因此可以使用副作用来检索索引,如下所示:

var idx;
_.find(tv, function(voteItem, voteIdx){ 
   if(voteItem.id == voteID){ idx = voteIdx; return true;}; 
});
方法2 查看下划线源代码,以下是如何实现
.find

_.find = _.detect = function(obj, predicate, context) {
  var result;
  any(obj, function(value, index, list) {
    if (predicate.call(context, value, index, list)) {
      result = value;
      return true;
    }
  });
  return result;
};

要使其成为
findIndex
函数,只需替换行
result=value带有
结果=索引这与第一个方法的想法相同。我加入它是为了指出下划线使用副作用来实现
查找。

findIndex
是在1.8中添加的:

index = _.findIndex(tv, function(voteItem) { return voteItem.id == voteID })
见:

或者,如果您不介意创建另一个临时列表,这也可以:

index = _.indexOf(_.pluck(tv, 'id'), voteId);
请参阅:

,它扩展了下划线,并有一个方法,可以找到给定实例的索引,或通过给定谓词,或根据给定对象的属性

就你而言,我会:

var index = _.findIndex(tv, { id: voteID });

试试看。

如果您希望有多个匹配项,因此需要返回一个数组,请尝试:

_.where(Users, {age: 24})
如果属性值是唯一的,并且需要匹配的索引,请尝试:

_.findWhere(Users, {_id: 10})
Array.prototype.getIndex=function(obj){
for(var i=0;i
如果您的目标环境支持ES2015(或者您有一个transfile步骤,例如使用Babel),则可以使用本机Array.prototype.findIndex()

以你为例


您可以使用
lodash中的
indexOf
方法

var tv = [{id:1},{id:2}]
var voteID = 2;
var data = _.find(tv, function(voteItem){ return voteItem.id == voteID; });
var index=_.indexOf(tv,data);

这是为了帮助
lodash
用户。通过执行以下操作检查您的钥匙是否存在:

hideSelectedCompany(yourKey) {
 return _.findIndex(yourArray, n => n === yourKey) === -1;
}

最简单的解决方案是使用lodash:

  • 安装lodash:
  • npm安装--保存lodash

  • 使用findIndex方法:
  • const=require('lodash')


    我得到了类似的情况,但相反的是根据给定对象的索引查找所用的键。我可以在下划线中找到解决方案,使用
    Object.values
    将对象返回到数组中以获取发生的索引

    var-tv={id1:1,id2:2};
    var voteIndex=1;
    console.log(u.findKey(电视、功能(项目)){
    返回u.indexOf(Object.values(tv),item)=voteIndex;
    }));
    
    可能重复感谢-这很有用-但在所列示例中,您正在搜索数字数组中的单个数字-我正在查找对象数组中的键值。这个函数是否适用?无论它是数字测试还是属性均衡器谓词都不重要,是的。尝试<代码> FixDeals<代码>:<代码> var DATAN DEXX=.FixDead(TV,{ID:VoTIDID })<代码>请考虑接受,如问题中的请求。最好是<代码>返回PARSETIN(i),当数组为x时,数组x为x(x)。
    这比以前的答案简单多了??findIntex这是一个来自lo dash的方法,而不是来自underscore@WallyssonNunes这正是我写的——“lodash扩展了下划线,具有findIndex方法”这现在是标准下划线库的一部分,从版本1.8.0开始:为什么不使用
    return-1默认设置?我不确定下划线是否正确,但在lodash中,您不需要匿名函数,只需
    index=.\ux.findIndex(tv,{id:voteID})
    就可以了,如果
    tv
    集合有更复杂的值(不仅仅是
    id
    属性),那么也可以了。使用pull的速度要慢得多。因为它有额外的横向移动,所以我很高兴。
    var tv = [{id:1},{id:2}]
    var voteID = 2;
    var data = _.find(tv, function(voteItem){ return voteItem.id == voteID; });
    var index=_.indexOf(tv,data);
    
    hideSelectedCompany(yourKey) {
     return _.findIndex(yourArray, n => n === yourKey) === -1;
    }
    
    findIndexByElementKeyValue = (elementKeyValue) => {
      return _.findIndex(array, arrayItem => arrayItem.keyelementKeyValue);
    }