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

时间:2016-04-05 06:56:43

标签: 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"
}]

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

var q = MY_JSON_STRING
console.log(q.indexOf( 'allInterests' ) );

谢谢和问候, Tismon Varghese

6 个答案:

答案 0 :(得分:23)

您必须使用Array.findArray.filterArray.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);




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);




默认Array.indexOf()会将searchValue与当前元素匹配,而不是其属性。您可以在MDN上提及Array.indexOf - polyfill

答案 1 :(得分:5)

您可以使用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);

希望有所帮助:)

答案 2 :(得分:0)

遍历数组并找到包含键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"));

答案 3 :(得分:0)

基于函数的解决方案,用于通过VanillaJS的值从JSON对象获取索引。

示例: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>

答案 4 :(得分:0)

一旦有了json对象

obj.valueOf(Object.keys(obj).indexOf('String_to_Find'))

答案 5 :(得分:0)

在所有以前的解决方案中,您必须知道属性或字段的名称。对于任何属性,更通用的解决方案是:

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)