29

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' ) );
1
  • 1
    What you posted is a plain old JavaScript array, not a JSON string. Array.prototype.indexOf() isn't doing what you think it is. Commented Apr 5, 2016 at 7:01

7 Answers 7

50

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

Sign up to request clarification or add additional context in comments.

2 Comments

It would make more sense to use Array.findIndex() since OP wants the index of an element.
You are correct, but I assumed, OP would access object after fetching index, so used Array.find
27

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

Comments

4

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

1 Comment

Why reinvent the wheel. There's already Array.prototype.findIndex that does the same job as your getIndexOf.
2

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)

Comments

1

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>

Comments

0

Once you have a json object

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

Comments

0

var MYarry=["13","18","77","4543"];
for (var i=0;i<=MYarry.length;i++){
           if(MYarry[i] === "18") {
           console.log(i);
           }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.