0

I am trying to put different values inside inside different variable where values are produced by outer loop and use that variable like array

var code = [];
for (i = 0; i < 5; i++) {
   code[i] = mp3;
}

How can I access variable like this:- code[1] , code[2];

Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
NIrik Shan
  • 25
  • 1
  • 6
  • Possible duplicate of [Loop through an array in JavaScript](http://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript) – Kindle Q Apr 10 '17 at 09:36

3 Answers3

0

You can push the value of mp3 into the array by using .push() as below. Then you can access it by the index of the array you want.

Note that you need to declare what mp3 is - I didn't know if it related to another variable so I just created a test value for it.

var code = [];
var mp3 = "test";
  for (i = 0; i < 5; i++) {
     code.push(mp3+i);
  }

console.log(code) // gives ["test0","test1","test2","test3","test4"]
console.log(code[0]) // gives test0
gavgrif
  • 15,194
  • 2
  • 25
  • 27
0

Is that what you want

var code = [];
for (i = 0; i < 5; i++) {
   code[i] = mp+"i";
}
alert(code[0])
code[0] will mp0
RITESH ARORA
  • 487
  • 3
  • 11
0

If the values are stored as per above loop , you can simply call code[0] or code[1] , if you are looking for some other information , please provide more details

Div
  • 99
  • 1
  • 1
  • 7