0

I have three arrays in my script, and I want to push value into one of them, the name of an array is determined of another variable value.

For example: variable value is "3" and I want to push "hello" into wals"3" array.

var wals1 = [];
var wals2 = [];
var wals3 = [];

var res = 3;

wals + res.push("hello");


console.log(wals3);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
KrystianK
  • 117
  • 11

1 Answers1

0

You can create a JSON object and then assign the values to the arrays by their dynamic name like this:

var arrayObj = {
 wals1 : [],
  wals2 : [],
  wals3 : []
};
var res = 3;

arrayObj["wals"+res].push("hello");
console.log(arrayObj["wals"+res]);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62