javascript - Declaring array of objects -
i have variable array , want every element of array act object default. achieve this, can in code.
var sample = new array(); sample[0] = new object(); sample[1] = new object();
this works fine, don't want mention index number. want elements of array object. how declare or initialize it?
var sample = new array(); sample[] = new object();
i tried above code doesn't work. how initialize array of objects without using index number?
use array.push()
add item end of array.
var sample = new array(); sample.push(new object());
to n
times use for
loop.
var n = 100; var sample = new array(); (var = 0; < n; i++) sample.push(new object());
note can substitute new array()
[]
, new object()
{}
becomes:
var n = 100; var sample = []; (var = 0; < n; i++) sample.push({});
Comments
Post a Comment