Consider the code snippet below:
for(i=0; i<3; i++){
a = {};
a['name' + i] = i;
data.push(a);
}
This code will generate the following array:
{
1:{name0:0},
2:{name1:1},
3:{name2:2}
}
How can I modify the code so that it produces the array in this format:
{
name0:0,
name1:1,
name2:2
}
The reason behind this adjustment is to be able to conveniently access array elements by name, such as data[name1]
, rather than having to search through the entire array.