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


90

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

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

但是如何找到该对象发生在哪个数组索引?



谢谢-这很有用-但是在列出的示例中,您正在搜索数字数组中的单个数字-我正在寻找对象数组中的键值。这个功能可以容纳吗?
mheavers 2014年

2
是数字测试还是财产资格谓词都没有关系。
Bergi 2014年

试试findIndexvar dataIndex = _.findIndex(tv, { id: voteID })
TomSöderlund'17年

Answers:


28

我不知道是否存在执行此操作的下划线方法,但是您可以使用纯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)]


4
为什么不return -1;默认?
Radu Chiriac

171

findIndex 在1.8中添加:

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

请参阅:http//underscorejs.org/#findIndex

另外,如果您不介意制作另一个临时列表,也可以使用此方法:

index = _.indexOf(_.pluck(tv, 'id'), voteId);

请参阅:http//underscorejs.org/#pluck


我不确定下划线,但是在lodash中,您不需要匿名函数,index = _.findIndex(tv, {id: voteID})它将可以正常工作,并且如果tv集合具有更复杂的值(不仅仅是一个id属性)也可以工作
Scott Jungwirth 2015年

1
仅仅使用pluck一点要慢得多。我因为它的额外遍历。jsperf.com/compare-find-index
Farshid Saberi,

38

如果您想使用下划线,以便您的谓词功能可以更灵活,请参考以下两个想法。

方法1

由于for的谓词_.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;result = index;这与第一种方法的思路相同。我将其包括在内以指出下划线也使用副作用来实现_.find


37

扩展Underscore的Lo-Dash具有findIndex方法,该方法可以找到给定实例的索引,给定谓词或根据给定对象的属性找到索引。

在您的情况下,我会这样做:

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

试试看。


findIntex这不是下划线而不是下划线的方法
Wallysson Nunes

4
@WallyssonNunes这正是我写的-“ Lo-Dash,扩展了Underscore,具有findIndex方法”
splintor 2015年

3
从1.8.0版开始,它现在是标准下划线库的一部分:underscorejs.org/#1.8.0
Gage Trader 2015年

10

如果您的目标环境支持ES2015(或者您具有转换步骤,例如使用Babel),则可以使用本机Array.prototype.findIndex()。

给出你的例子

const array = [ {id:1}, {id:2} ]
const desiredId = 2;
const index = array.findIndex(obj => obj.id === desiredId);

4

您可以使用以下indexOf方法lodash

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

3

保持简单:

// 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
更好的是return parseInt(i) for i, x of array when cond(x)
meagar

1
这比以前的答案简单吗?
ncubica 2015年

2

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

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

1

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

  1. 安装lodash:

npm install-保存lodash

  1. 使用方法findIndex:

const _ = require('lodash');

findIndexByElementKeyValue = (elementKeyValue) => {
  return _.findIndex(array, arrayItem => arrayItem.keyelementKeyValue);
}

0

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

_.where(Users, {age: 24})

如果属性值是唯一的,并且您需要匹配的索引,请尝试:

_.findWhere(Users, {_id: 10})


0

我有类似的情况,但相反是基于给定对象的索引来找到使用的密钥。我能找到解决办法下划线使用Object.values到返回对象到一个数组,以获得发生指数。

var tv = {id1:1,id2:2};
var voteIndex = 1;
console.log(_.findKey(tv, function(item) {
  return _.indexOf(Object.values(tv), item) == voteIndex;
}));
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.