[英]find the array index of an object with a specific key value in underscore
在下划線中,我可以成功找到具有特定鍵值的項目
var tv = [{id:1},{id:2}]
var voteID = 2;
var data = _.find(tv, function(voteItem){ return voteItem.id == voteID; });
//data = { id: 2 }
但是我如何找到 object 出現的數組索引?
findIndex
在1.8中添加:
index = _.findIndex(tv, function(voteItem) { return voteItem.id == voteID })
請參閱: http : //underscorejs.org/#findIndex
或者,如果您不介意制作另一個臨時列表,這也有效:
index = _.indexOf(_.pluck(tv, 'id'), voteId);
如果你想留下下划線,那么你的謂詞函數可以更靈活,這里有2個想法。
由於_.find
的謂詞_.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;
with result = index;
這與第一種方法的想法相同。 我把它包括在內,指出下划線使用副作用來實現_.find
。
Lo-Dash擴展了Underscore ,它有findIndex方法,可以找到給定實例的索引,或者通過給定的謂詞,或者根據給定對象的屬性。
在你的情況下,我會這樣做:
var index = _.findIndex(tv, { id: voteID });
試試看。
我不知道是否存在執行此操作的現有下划線方法,但您可以使用普通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)]
如果您的目標環境支持ES2015(或者您有一個簡單的步驟,例如使用Babel),則可以使用本機Array.prototype.findIndex()。
舉個例子
const array = [ {id:1}, {id:2} ]
const desiredId = 2;
const index = array.findIndex(obj => obj.id === desiredId);
你可以使用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);
保持簡單:
// 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)
如果您期望多個匹配,因此需要返回一個數組,請嘗試:
_.where(Users, {age: 24})
如果屬性值是唯一的並且您需要匹配的索引,請嘗試:
_.findWhere(Users, {_id: 10})
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);
這是為了幫助lodash
用戶。 通過以下方式檢查您的密鑰是否存在:
hideSelectedCompany(yourKey) {
return _.findIndex(yourArray, n => n === yourKey) === -1;
}
最簡單的解決方案是使用lodash:
npm install --save lodash
const _ = require('lodash');
findIndexByElementKeyValue = (elementKeyValue) => {
return _.findIndex(array, arrayItem => arrayItem.keyelementKeyValue);
}
我有類似的情況,但相反是根據給定對象的索引找到使用過的鍵。 我可以在下划線中找到解決方案,使用Object.values
將 object 返回到數組中以獲取發生的索引。
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>
如果你想通過 id 從 object 中獲得一些特定的鍵值,那么使用
var tv = [{id:1, name:"ABC"},{id:2, name:xyz}]
_.find(tv , {id:1}).value // retrun "ABC"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.