Monday, February 25, 2019

Get index from a JSON object with value

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"}];

// Google Chrome user.
var index = data.findIndex(obj => obj.name=="allInterests");

console.log(index);


// for IE user

var index = -1;
for (var i = 0; i < data.length; ++i) {
    if (data[i].name == "allInterests") {
        index = i;
        break;
    }

}

console.log(index);

No comments:

Post a Comment

Your feedback is always appreciated. I will try to reply to your queries as soon as time allows.Please don't spam,spam comments will be deleted upon reviews.