EN
JavaScript - declare array of objects
0 points
In this article, we would like to show you how to declare an array of objects in JavaScript.
In this example, we declare an array
and present how to push a single object into it.
xxxxxxxxxx
1
var array = new Array();
2
3
array.push(new Object());
4
5
console.log(JSON.stringify(array)); // [{}]
In this section, we present how to create an actual array of objects by pushing multiple objects into the array
.
xxxxxxxxxx
1
var array = new Array();
2
3
var n = 3;
4
5
for (var i = 0; i < n; ++i) {
6
array.push(new Object());
7
}
8
9
console.log(JSON.stringify(array)); // [{},{},{}]
In this example, we present how to declare an array of objects by simply adding new objects to the array
on creation.
xxxxxxxxxx
1
const array = [new Object(), new Object(), new Object()];
2
3
console.log(JSON.stringify(array)); // [{},{},{}]
or:
xxxxxxxxxx
1
const array = [{}, {}, {}];
2
3
console.log(JSON.stringify(array)); // [{},{},{}]