Javascript 从具有值的 JSON 对象中获取索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36419195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get index from a JSON object with value
提问by Tismon Varghese
This is my json string.
这是我的 json 字符串。
[{
"name": "placeHolder",
"section": "right"
}, {
"name": "Overview",
"section": "left"
}, {
"name": "ByFunction",
"section": "left"
}, {
"name": "Time",
"section": "left"
}, {
"name": "allFit",
"section": "left"
}, {
"name": "allbMatches",
"section": "left"
}, {
"name": "allOffers",
"section": "left"
}, {
"name": "allInterests",
"section": "left"
}, {
"name": "allResponses",
"section": "left"
}, {
"name": "divChanged",
"section": "right"
}]
Now, i have the value allInterests
and i want to find out the index (this case; it is '7') of this object in the above string. I tried the following code; but it always returns -1. Can someone help me to findout where i went wrong ?
现在,我有了这个值allInterests
,我想在上面的字符串中找出这个对象的索引(这种情况下,它是“7”)。我尝试了以下代码;但它总是返回-1。有人可以帮我找出我出错的地方吗?
var q = MY_JSON_STRING
console.log(q.indexOf( 'allInterests' ) );
Thanks and regards, Tismon Varghese
谢谢和问候,蒂斯蒙·瓦格塞
回答by Rajesh
You will have to use Array.find
or Array.filter
or Array.forEach
.
您将不得不使用Array.find
或Array.filter
或Array.forEach
。
Since you value is array and you need position of element, you will have to iterate over it.
由于您的值是数组并且您需要元素的位置,因此您必须对其进行迭代。
Array.find
数组查找
var data = [{"name":"placeHolder","section":"right"},{"name":"Overview","section":"left"},{"name":"ByFunction","section":"left"},{"name":"Time","section":"left"},{"name":"allFit","section":"left"},{"name":"allbMatches","section":"left"},{"name":"allOffers","section":"left"},{"name":"allInterests","section":"left"},{"name":"allResponses","section":"left"},{"name":"divChanged","section":"right"}];
var index = -1;
var val = "allInterests"
var filteredObj = data.find(function(item, i){
if(item.name === val){
index = i;
return i;
}
});
console.log(index, filteredObj);
Array.findIndex() @Ted Hopp's suggestion
Array.findIndex() @ Ted Hopp的建议
var data = [{"name":"placeHolder","section":"right"},{"name":"Overview","section":"left"},{"name":"ByFunction","section":"left"},{"name":"Time","section":"left"},{"name":"allFit","section":"left"},{"name":"allbMatches","section":"left"},{"name":"allOffers","section":"left"},{"name":"allInterests","section":"left"},{"name":"allResponses","section":"left"},{"name":"divChanged","section":"right"}];
var val = "allInterests"
var index = data.findIndex(function(item, i){
return item.name === val
});
console.log(index);
Default Array.indexOf()
will match searchValue to current element and not its properties. You can refer Array.indexOf - polyfillon MDN
默认Array.indexOf()
将 searchValue 匹配到当前元素而不是它的属性。你可以参考MDN 上的Array.indexOf - polyfill
回答by Sarat Chandra
You can use Array.findIndex.
您可以使用 Array.findIndex。
var data= [{
"name": "placeHolder",
"section": "right"
}, {
"name": "Overview",
"section": "left"
}, {
"name": "ByFunction",
"section": "left"
}, {
"name": "Time",
"section": "left"
}, {
"name": "allFit",
"section": "left"
}, {
"name": "allbMatches",
"section": "left"
}, {
"name": "allOffers",
"section": "left"
}, {
"name": "allInterests",
"section": "left"
}, {
"name": "allResponses",
"section": "left"
}, {
"name": "divChanged",
"section": "right"
}];
var index = data.findIndex(obj => obj.name=="allInterests");
console.log(index);
Hope it helps :)
希望能帮助到你 :)
回答by void
Traverse through the array and find the index of the element which contains a key name
and has the value as the passed param.
遍历数组并找到包含键name
并具有作为传递参数的值的元素的索引。
var data = [{
"name": "placeHolder",
"section": "right"
}, {
"name": "Overview",
"section": "left"
}, {
"name": "ByFunction",
"section": "left"
}, {
"name": "Time",
"section": "left"
}, {
"name": "allFit",
"section": "left"
}, {
"name": "allbMatches",
"section": "left"
}, {
"name": "allOffers",
"section": "left"
}, {
"name": "allInterests",
"section": "left"
}, {
"name": "allResponses",
"section": "left"
}, {
"name": "divChanged",
"section": "right"
}];
Array.prototype.getIndexOf = function(el) {
var arr = this;
for (var i=0; i<arr.length; i++){
console.log(arr[i].name);
if(arr[i].name==el){
return i;
}
}
return -1;
}
alert(data.getIndexOf("allResponses"));
回答by GMK Hussain
Function base solution for get index from a JSON object with value by VanillaJS.
VanillaJS 从具有值的 JSON 对象获取索引的函数基础解决方案。
Exemple: https://codepen.io/gmkhussain/pen/mgmEEW
示例:https: //codepen.io/gmkhussain/pen/mgmEEW
var data= [{
"name": "placeHolder",
"section": "right"
}, {
"name": "Overview",
"section": "left"
}, {
"name": "ByFunction",
"section": "left"
}, {
"name": "Time",
"section": "left"
}, {
"name": "allFit",
"section": "left"
}, {
"name": "allbMatches",
"section": "left"
}, {
"name": "allOffers",
"section": "left"
}, {
"name": "allInterests",
"section": "left"
}, {
"name": "allResponses",
"section": "left"
}, {
"name": "divChanged",
"section": "right"
}];
// create function
function findIndex(jsonData, findThis){
var indexNum = jsonData.findIndex(obj => obj.name==findThis);
//Output of result
document.querySelector("#output").innerHTML=indexNum;
console.log(" Array Index number: " + indexNum + " , value of " + findThis );
}
/* call function */
findIndex(data, "allOffers");
Output of index number : <h1 id="output"></h1>
回答by Saurav
Once you have a json object
一旦你有一个 json 对象
obj.valueOf(Object.keys(obj).indexOf('String_to_Find'))
回答by Jesús Gabriel Banda Durán
In all previous solutions, you must know the name of the attribute or field. A more generic solution for any attribute is this:
在之前的所有解决方案中,您必须知道属性或字段的名称。对于任何属性,更通用的解决方案是:
let data =
[{
"name": "placeHolder",
"section": "right"
}, {
"name": "Overview",
"section": "left"
}, {
"name": "ByFunction",
"section": "left"
}, {
"name": "Time",
"section": "left"
}, {
"name": "allFit",
"section": "left"
}, {
"name": "allbMatches",
"section": "left"
}, {
"name": "allOffers",
"section": "left"
}, {
"name": "allInterests",
"section": "left"
}, {
"name": "allResponses",
"section": "left"
}, {
"name": "divChanged",
"section": "right"
}]
function findByKey(key, value) {
return (item, i) => item[key] === value
}
let findParams = findByKey('name', 'allOffers')
let index = data.findIndex(findParams)