How can I add an object to a new empty array, in a specific index.
Let's say "0"
I have a forEach loop and I build an object with it
var array = [];
var myObject = {
value1: value1,
value2: value2
};
I can generate as many "myObject" as the times the forEach loops
How can I add all those "myObject" into "array" on the index "0"?
Square brackets to the rescue:
var obj = {}
for (var i = 0; i < 5; i++) {
if (!obj["val"+i]) {
obj["val"+i] = []
}
obj["val"+i].push("val"+i)
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.