How to create array of objects in javascript?

2

I am a beginner in javascript and would like to know if it is possible to create array of objects in javascript, if so, how do I do this? I know that to create an object I need to do the following:

var Ponto = function (latitude, longitude) {
    this.latitude = latitude;
    this.longitude = longitude;
}

var ponto = new Ponto(80,50);

My intention now was to create an array of points to work with, is that possible? Thank you in advance for your cooperation!

    
asked by anonymous 29.07.2017 / 00:07

1 answer

5

Use array.push() to add an item to the end of the array.

var sample = new Array();
sample.push(new Object());

To do this "n" times using loop:

var n = 100;
var sample = new Array();
for (var i = 0; i < n; i++)
    sample.push(new Object());
    
29.07.2017 / 00:14