在下划线下,我可以成功找到具有特定键值的项目
var tv = [{id:1},{id:2}]
var voteID = 2;
var data = _.find(tv, function(voteItem){ return voteItem.id == voteID; });
//data = { id: 2 }
但是如何找到该对象发生在哪个数组索引?
在下划线下,我可以成功找到具有特定键值的项目
var tv = [{id:1},{id:2}]
var voteID = 2;
var data = _.find(tv, function(voteItem){ return voteItem.id == voteID; });
//data = { id: 2 }
但是如何找到该对象发生在哪个数组索引?
findIndex
:var dataIndex = _.findIndex(tv, { id: voteID })
Answers:
我不知道是否存在执行此操作的下划线方法,但是您可以使用纯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)]
return -1;
默认?
findIndex
在1.8中添加:
index = _.findIndex(tv, function(voteItem) { return voteItem.id == voteID })
请参阅:http://underscorejs.org/#findIndex
另外,如果您不介意制作另一个临时列表,也可以使用此方法:
index = _.indexOf(_.pluck(tv, 'id'), voteId);
index = _.findIndex(tv, {id: voteID})
它将可以正常工作,并且如果tv
集合具有更复杂的值(不仅仅是一个id
属性)也可以工作
如果您想使用下划线,以便您的谓词功能可以更灵活,请参考以下两个想法。
由于for的谓词_.find
同时接收元素的值和索引,因此可以使用副作用来检索索引,如下所示:
var idx;
_.find(tv, function(voteItem, voteIdx){
if(voteItem.id == voteID){ idx = voteIdx; return true;};
});
查看下划线来源,这是如何_.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
。
扩展Underscore的Lo-Dash具有findIndex方法,该方法可以找到给定实例的索引,给定谓词或根据给定对象的属性找到索引。
在您的情况下,我会这样做:
var index = _.findIndex(tv, { id: voteID });
试试看。
如果您的目标环境支持ES2015(或者您具有转换步骤,例如使用Babel),则可以使用本机Array.prototype.findIndex()。
给出你的例子
const array = [ {id:1}, {id:2} ]
const desiredId = 2;
const index = array.findIndex(obj => obj.id === desiredId);
您可以使用以下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);
保持简单:
// 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)
return parseInt(i) for i, x of array when cond(x)
Array.prototype.getIndex = function (obj) {
for (var i = 0; i < this.length; i++) {
if (this[i][Id] == obj.Id) {
return i;
}
}
return -1;
}
List.getIndex(obj);
我有类似的情况,但相反是基于给定对象的索引来找到使用的密钥。我能找到解决办法下划线使用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>