How to Add Object to Array in JavaScript
- Add Items and Objects to an Array Using the Assignment Operator in JavaScript
-
Add Items and Objects to an Array Using the
push()
Function in JavaScript
This tutorial will discuss adding items and objects to an array using the assignment operator and the push()
function in JavaScript.
Add Items and Objects to an Array Using the Assignment Operator in JavaScript
To add items and objects to an array, you can use the assignment operator in JavaScript. You have to use the index to define the position inside the array where you want to put the item or object. If an existing item already occupies the defined index, the item will be replaced with the new item or object. For example, let’s create an array with three values and add an item at the end of the array using the assignment operator. See the code below.
var myArray = ['one', 'two', 'three'];
myArray[3] = 'four';
console.log(myArray)
Output:
["one", "two", "three", "four"]
In the above code, we added the item four
at index 3 of the myArray
. You can also replace the items present in the array using their index. Now let’s add an object to an array. See the code below.
var myArray = ['one', 'two', 'three'];
var myArray2 = ['four', 'five']
myArray[3] = myArray2;
console.log(myArray)
Output:
["one", "two", "three", Array(2)]
In the above code, we added an array object myArray2
to an array myArray
at index 3. You can add objects of any data type to an array using the assignment operator.
Add Items and Objects to an Array Using the push()
Function in JavaScript
To add items and objects to an array, you can use the push()
function in JavaScript. The push()
function adds an item or object at the end of an array. For example, let’s create an array with three values and add an item at the end of the array using the push()
function. See the code below.
var myArray = ['one', 'two', 'three'];
myArray.push('four');
console.log(myArray)
Output:
["one", "two", "three", "four"]
In the above code, we added the item four
at the end of the myArray
. Now let’s add an object to an array using the push()
function. See the code below.
var myArray = ['one', 'two', 'three'];
var myArray2 = ['four', 'five']
myArray.push(myArray2);
console.log(myArray)
Output:
["one", "two", "three", Array(2)]
In the above code, we added an array object myArray2
to an array myArray
at the end. You can add objects of any data type to an array using the push()
function. You can also add multiple values to an array by adding them in the push()
function separated by a comma. To add the items or objects at the beginning of the array, we can use the unshift()
function. For example, let’s add the item four
at the beginning of the array myArray
. See the code below.
var myArray = ['one', 'two', 'three'];
myArray.unshift('four');
console.log(myArray)
Output:
["four", "one", "two", "three"]
As you can see in the output, the item four
is added at the beginning of the array. Instead of adding an array object, you can add all of its items using the push.apply()
function. For example, let’s add the items present in one array to the other array. See the code below.
var myArray = ['one', 'two', 'three'];
var myArray2 = ['four', 'five']
myArray.push.apply(myArray, myArray2);
console.log(myArray)
Output:
["one", "two", "three", "four", "five"]
As you can see in the output, the two items present in the myArray2
have been added to the myArray
. You can also concatenate two arrays to make another array using the concat()
function. For example, let’s create an array by concatenating two existing arrays using the concat()
function. See the code below.
var myArray = ['one', 'two', 'three'];
var myArray2 = ['four', 'five'];
var myArray3 = myArray.concat(myArray2);;
console.log(myArray3)
Output:
["four", "five", "one", "two", "three"]
You can change the order of the items present in the myArray3
by changing the order of concatenation.
Related Article - JavaScript Array
- How to Check if Array Contains Value in JavaScript
- How to Create Array of Specific Length in JavaScript
- How to Convert Array to String in JavaScript
- How to Remove First Element From an Array in JavaScript
- How to Search Objects From an Array in JavaScript
- How to Convert Arguments to an Array in JavaScript