javascript - set custom JSON object names in a for loop -
how set custom json object name in loop.
var myarray = []; (var = 0; i<= 8; i++){ var x = "name" + i; myarray.push({x:[0,0,0]}); }
i want this
myarray = [ {name0:[0,0,0]}, {name1:[0,0,0]}, {name2:[0,0,0]}, {name3:[0,0,0]}, {name4:[0,0,0]}, {name5:[0,0,0]}, {name6:[0,0,0]}, {name7:[0,0,0]}, {name8:[0,0,0]}, ];
but returns this
myarray = [ {x:[0,0,0]}, {x:[0,0,0]}, {x:[0,0,0]}, {x:[0,0,0]}, {x:[0,0,0]}, {x:[0,0,0]}, {x:[0,0,0]}, {x:[0,0,0]}, {x:[0,0,0]}, ];
bracket notation:
for (var = 0; i<= 8; i++){ var obj = {}; obj['name'+ i] = [0,0,0]; myarray.push(obj); }
Comments
Post a Comment