How to add an object to an array
Put anything into an array using Array.push().
var a=[], b={};a.push(b); // a[0] === b;
Extra information on Arrays
Add more than one item at a time
var x = ['a'];x.push('b', 'c');// x = ['a', 'b', 'c']
Add items to the beginning of an array
var x = ['c', 'd'];x.unshift('a', 'b');// x = ['a', 'b', 'c', 'd']
Add the contents of one array to another
var x = ['a', 'b', 'c'];var y = ['d', 'e', 'f'];x.push.apply(x, y);// x = ['a', 'b', 'c', 'd', 'e', 'f']// y = ['d', 'e', 'f'] (remains unchanged)
Create a new array from the contents of two arrays
var x = ['a', 'b', 'c'];var y = ['d', 'e', 'f'];var z = x.concat(y);// x = ['a', 'b', 'c'] (remains unchanged)// y = ['d', 'e', 'f'] (remains unchanged)// z = ['a', 'b', 'c', 'd', 'e', 'f']
var years = [];for (i= 2015;i<=2030;i=i+1){years.push({operator : i})}
here array years is having values like
years[0]={operator:2015}years[1]={operator:2016}
it continues like this.
First of all, there is no object
or array
. There are Object
and Array
. Secondly, you can do that:
a = new Array();b = new Object();a[0] = b;
Now a
will be an array with b
as its only element.