How to add an object to an array

How to add an object to an array

How can I add an object to an array (in javascript or jquery)?
For example, what is the problem with this code?
var a = new array();
var b = new object();

I would like to use this code to save many objects in the array of function1 and call function2 to use the object in the array.

How can I save an object in an array?
How can I put an object in an array and save it to a variable?


Solution 1:

Put anything into an array using Array.push().

var a=[], 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']

Solution 2:

var years = [];
for (i= 2015;i<=2030;i=i+1)
years.push({operator : i})

here array years is having values like


it continues like this.

Solution 3:

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.

Solution 4:

  • JavaScript is case-sensitive. Calling new array() and new object() will throw a ReferenceError since they don’t exist.
  • It’s better to avoid new Array() due to its error-prone behavior.
    Instead, assign the new array with = [val1, val2, val_n]. For objects, use = {}.
  • There are many ways when it comes to extending an array (as shown in John’s answer) but the safest way would be just to use concat instead of push. concat returns a new array, leaving the original array untouched. push mutates the calling array which should be avoided, especially if the array is globally defined.
  • It’s also a good practice to freeze the object as well as the new array in order to avoid unintended mutations. A frozen object is neither mutable nor extensible (shallowly).

Applying those points and to answer your two questions, you could define a function like this:

function appendObjTo(thatArray, newObj) {
  const frozenObj = Object.freeze(newObj);
  return Object.freeze(thatArray.concat(frozenObj));


// Given
const myArray = ["A", "B"];
// "save it to a variable"
const newArray = appendObjTo(myArray, {hello: "world!"});
// returns: ["A", "B", {hello: "world!"}]. myArray did not change.

Solution 5:

Using ES6 notation, you can do something like this:

For appending you can use the spread operator like this:

var arr1 = [1,2,3]
var obj = 4
var newData = [...arr1, obj] // [1,2,3,4]

Solution 6:

Expanding Gabi Purcaru’s answer to include an answer to number 2.

a = new Array();
b = new Object();
a[0] = b;

var c = a[0]; // c is now the object we inserted into a...