javaScript push array method; In this tutorial, you will learn how to add/insert elements/items or array into array uisng javaScript array push() method.
Sometimes you want to push single or multiple items in an array. So you can use the JavaScript push() method to add elements in an array.
If you work with javascript arrays, you should read this javascript array posts:
- javaScript Push Element, Array Into Array Example
- JavaScript: Multidimensional Array With Push Pop
- Remove Duplicate Elements From Array in JavaScript
javaScript push() Method
The javaScript push() method is used to add a new element to the end of an array.
Note: javaScript push() array method changes the length of the given array.
Syntax
array.push(item1, item2, ..., itemX)
Parameter with description
Parameter | Description |
---|---|
item1, item2, …, itemX | Ii is required. The item(s) to add to the array |
Here you will learn the following:
- How to add the single item of the array?
- How to add the multiple items of the array?
- How to merge two array?
- Javascript push array into an array.
How to add the single item of the array?
Here, you will learn how to add a single item into a given array. Let’s take an example.
Suppose we have an array that’s the name arrNum, and it contains four elements inside it, see below:
var arrNum = [ "one", "two", "three", "four" ];
If you want to add the single item into the arryNum array. So you can use the push() method of javaScript like below:
var arrNum = [ "one", "two", "three", "four" ]; arrNum.push("five"); console.log( arrNum );
The result of the above example is:
["one", "two", "three", "four", "five"]
How to add the multiple items of the array?
If you want to add multiple items and elements into a given array. You can use the below example.
Let’s take a new example of how to add multiple items into a given array.
Suppose we have an array that names arrMul and it contains five value and we want to add five more or N value into it, so use the below example
var arrMul = [ "one", "two", "three", "four" ]; arrMul.push("six","seven","eight","nine","ten"); console.log( arrMul );
The result of the above example is:
["one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
How to merge two array?
If you want to merge two array in javaScript. So you can use the apply() method.
Suppose, you have two array and you want to merge, let’s take an example:
const numbers = ['1', '2']; const newNumbers = ['3', '4', '5']; Array.prototype.push.apply(numbers, newNumbers); console.log(numbers);
Note: If the second array (newNumbers in the example) is huge, do not use the apply () method, because the maximum number of arguments a function can handle is limited in practice.
The result of the above example is:
["1", "2", "3", "4", "5"]
Javascript push array into array
If you want to add array into another array. Let’s see the below example:
var arr = [ ["one"], ["two"], ["three"], ["four"], ["five"] ]; arr.push( ["six"], ["seven"], ["eight"], ["nine"], ["ten"] ); console.log(arr);
The result of the above example is:
[Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1), Array(1)]
javascript array push object
How to push an array of the object into array.
Let’s take a simple example of this, suppose we have one an arrayObj and it contains two object. If you want to add one more object into the arrayObj. So you can see the below example.
arrayObj = [ { name: 'xyz', email: '[email protected]' }, { name: 'abc', email: '[email protected]' } ]; arrayObj.push({ name: 'test', email: '[email protected]' }); console.log(arrayObj);
Conclusion
In this article, you have learned, how to add single and multiple items into a given array and how to merge two array. And also learn how to add array into another array.
You may like
- JavaScript Array splice() Method By Example
- Sorting Using JavaScript Array Sort() Method
- JavaScript: Clone an Array & Object By Examples
- JavaScript: Convert String to Array JavaScript
- Remove (First, last, specific) Element From Array Javascript
- javaScript Push Array, Items Into Array Example
- JavaScript Check the Object | isArray() Function
- JavaScript: Important Array Methods