Javascript Find Index of Array JSON

I have a JSON value which was converted into an array in javascript. Now how do I find the index value of the array containing the particular value I have?

Javascript Logo

The JSON Array.


var array2 = [
{"transactionCode": "0000000001",  "defaultLimit": "8000"},

{"transactionCode": "0000000002", , "defaultLimit": "5000"}

];

How do I know what is the index value of “0000000002”? From the above we know it is at index 1. While some may argue, why not do a loop to determine at which level, but on ECMA 5, there is no need to do that.


var val = "0000000002" // simple desired value
var filteredObj = array2.find(function(item, i){
if(item.transactionCode === val){
return i;
}
});

With this, you can finally know that the index is at 1.

Note: Another thing is that, it is not advisable to loop the whole thing. Imagine if there are thousand of records. Voila.

 

Leave a Reply