javaScript Push() Array – Add Element, Array into Array

javaScript Push() Array – Add Element, Array into Array

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() 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(item1item2, ..., itemX)

Parameter with description

ParameterDescription
item1item2, …, itemXIi 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

  1. JavaScript Array splice() Method By Example
  2. Sorting Using JavaScript Array Sort() Method
  3. JavaScript: Clone an Array & Object By Examples
  4. JavaScript: Convert String to Array JavaScript
  5. Remove (First, last, specific) Element From Array Javascript
  6. javaScript Push Array, Items Into Array Example
  7. JavaScript Check the Object | isArray() Function
  8. JavaScript: Important Array Methods

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *