Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the index from a JSON object with value?

This is my JSON string.

[{
    "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.

var q = MY_JSON_STRING
console.log(q.indexOf( 'allInterests' ) );
like image 496
Tismon Varghese Avatar asked Apr 05 '16 06:04

Tismon Varghese


People also ask

Does JSON have index?

You can index JSON data as you would any data of the type that you use to store it. In particular, you can use a B-tree index or a bitmap index for SQL/JSON function json_value , and you can use a bitmap index for SQL/JSON conditions is json , is not json , and json_exists .

How can I get specific data from JSON?

Getting a specific property from a JSON response object Instead, you select the exact property you want and pull that out through dot notation. The dot ( . ) after response (the name of the JSON payload, as defined arbitrarily in the jQuery AJAX function) is how you access the values you want from the JSON object.

What is JSON parse () method?

parse() The JSON. parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.


5 Answers

You will have to use Array.find or Array.filter or Array.forEach.

Since your value is array and you need the position of the 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

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 - polyfill on MDN

like image 197
Rajesh Avatar answered Oct 12 '22 02:10

Rajesh


You can use 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);
like image 23
Sarat Chandra Avatar answered Oct 12 '22 00:10

Sarat Chandra


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)
like image 28
Jesús Gabriel Banda Durán Avatar answered Oct 12 '22 01:10

Jesús Gabriel Banda Durán


Traverse through the array and find the index of the element which contains a key name and has the value as the passed param.

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"));
like image 42
void Avatar answered Oct 12 '22 02:10

void


Function base solution for get index from a JSON object with value by VanillaJS.

Exemple: 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>
like image 21
GMKHussain Avatar answered Oct 12 '22 02:10

GMKHussain