How to create object array in javascript?

Asked

Viewed 2,914 times

4

I’m a beginner in javascript and would like to know if it is possible to create object array in javascript, if yes, how do I do that? 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 be able to work with them, is that possible? I thank you in advance for your cooperation!

2 answers

7


Use the array.push() to add an item at 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());
  • 1

    It worked right here. Thank you very much!

0

Basically the array structure is this:

var nomeDoArray ["Aqui","dentro","você","coloca","os","elementos","que","podem","ser","strings","como","você","está","vendo"];

To view the array elements separately just print on console log. as below:

console.log(nomeDoArray[0]);

Items begin to be numbered from 0 in the array, then the first element of the array will be shown in this example, which in this case is the string element "Here".

You can only create arrays with elements of the same type, such as string array, numerical value array, etc. You cannot mix types.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.