0

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 works 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.

Nikolay Mihaylov
  • 3,868
  • 8
  • 27
  • 32
Paddy
  • 772
  • 2
  • 11
  • 28

1 Answers1

1

I think you want this:

$('.someDiv').append(
    theObject[i][finalM].someVal
);
user94559
  • 59,196
  • 6
  • 103
  • 103
  • 2
    There are hundreds of duplicates of this type of question/answer as this is a very commonly asked question/answer. – jfriend00 Aug 17 '16 at 05:48