从具有值的JSON对象获取索引

Tis*_*ese 10 javascript jquery json

这是我的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"
}]
Run Code Online (Sandbox Code Playgroud)

现在,我有了值allInterests,我想在上面的字符串中找到该对象的索引(这种情况;它是'7').我尝试了以下代码; 但它总是返回-1.有人能帮助我找出我哪里出错了吗?

var q = MY_JSON_STRING
console.log(q.indexOf( 'allInterests' ) );
Run Code Online (Sandbox Code Playgroud)

谢谢和问候,Tismon Varghese

Raj*_*esh 27

你必须使用Array.find或Array.filter或Array.forEach.

由于您的值是数组而您需要元素的位置,因此您必须迭代它.

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);
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

Default Array.indexOf()将使searchValue与当前元素匹配,而不是其属性.您可以在MDN上引用Array.indexOf - polyfill

  • 使用 [`Array.findIndex()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex) 会更有意义,因为 OP 想要索引的一个元素。 (2认同)

Sar*_*dra 7

您可以使用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);
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你 :)