0

I need to fill an array object as like below using integer variable.

for(i=1; i<=2; i++)

arr[i].push({i:(100 * i)})

Expected result is:

arr = [{ 1:100,2:200},{1:100,2:200}]

Problem is, array created as like below

arr = [{i:100,i:200},{i:100,i:200}]

prabu_s
  • 137
  • 1
  • 6

1 Answers1

0

You need to push to arr instead of arr[i]. Also, you can't use a variable as a key in json directly.

var arr = []; 
for(i=1; i<=2; i++)
{
    var b = {}; 
    b[i] = 100*i;
    arr.push({[i]:(i*100)});    
}
console.log(arr);
Saa
  • 1,540
  • 10
  • 22