Paddy Paddy - 3 months ago 7
JSON Question

passing a string to a function to get used as an object in javascript/jquery

I have a loop that calls a JSON array as an object. I want to itterate through the array to find items called match, the match names are called - match1, match2 etc. It woks instead of using the created var finalM i type match1, match2 etc.

var theObject = data; // get json data
var i;
for(i=0; i<theObject.length; i++){
var add = i + 1 ;
var finalM = "match"+ add;
$('.someDiv').append(
theObject[i].finalM.someVal
);
}


I know its something to do with the scope of the string, but can't find a good example to help me with the problem.

Answer

I think you want this:

$('.someDiv').append(
    theObject[i][finalM].someVal
);