AppDividend
Latest Code Tutorials

Javascript Array Push Example | Array.prototype.push() Tutorial

0

Javascript Array Push Example | Array.prototype.push() Tutorial is today’s leading topic.  The push() method adds a new item to the end of an array and returns the new length of an array. The push() method changes the length of the array or collection. If you want to add items at the beginning of an array, then use the javascript unshift() method. So, in this example, we will see how we can perform javascript add to array operation.

Javascript Array Push Example

Let us see the syntax of the Javascript Array push() method.

array.push(element)

The push() method appends the values to an array.

The push() method is intentionally generic. This method can be used with a call() or apply() on objects resembling the arrays. The push()  method relies on the length property to resolve to where to start inserting the given items. If the length cannot be converted into the number, the index used is 0. It includes the possibility of length being nonexistent, in which case the length will also be created.

#Adding elements to an array

Let us take an example of Node.js to understand how to add an item in an array in javascript.

// server.js

const songs = ['Dangerous', 'Thriller'];
const totalSongs = songs.push('Stranger In Moscow');

console.log(totalSongs);

console.log(songs);

 

Javascript Array Push Example | Array.prototype.push() Tutorial

#Merging two arrays

Let us take an example of how we can merge two arrays.

// server.js

const artists = ['michael', 'elvis'];
const newArtists = ['justin', 'charlie'];

Array.prototype.push.apply(artists, newArtists);

console.log(artists);

 

Array push() example tutorial

Do not use the apply() method if the second array (newArtists in the example) is vast, because the maximum number of arguments that one function can handle is limited in practice.

So, apply() will add the second array into the first array and we can see the combined array by returning the original array in our case it is artists.

We can also use the Javascript Array concat() method to merge the arrays.

#Add Multiple Items in an Array

See the following code. We have taken an array of Strings.

// server.js

const artists = ['michael', 'elvis'];

artists.push('justin', 'charlie');

console.log(artists); // ['michael', 'elvis', 'justin', 'charlie']

Here, we have added two more artists. Now, see the following code.

// server.js

const artists = ['michael', 'elvis'];
const newArtists = ['justin', 'charlie'];
artists.push(newArtists);

console.log(artists);

 

Javascript array push method

Here, the third element is an array. So it has added the third item as a whole array. So we can not combine it as we have done it with apply().

We can remove the element from an array using Javascript Array pop() method.

#Javascript push object to array

We can also add an array and the object to the array.

Here, See the following code.

// app.js

arrObj = [
  {
    name: 'Krunal',
    age: 26
  },
  {
    name: 'Ankit',
    age: 24
  }
];
arrObj.push({
  name: 'Rushabh',
  age: 27
});
console.log(arrObj);

See the following output.

Javascript push object to array

So, this is How to push both key and value into an Array in Javascript.

#Javascript push array into array

We can add an array into an array just like adding an element into the array.

// app.js

arrArray = [
  ['Krunal', 'Ankit'],
  ['Rushabh', 'Dhaval']
];
arrArray.push([
  'Tejash', 'Rajesh'
]);
console.log(arrArray);

See the output.

Javascript push array into array

#Javascript multidimensional array push

We can also add a multidimensional array into an array using a js array push function.

See the following programming example.

// app.js
 mulArray = [];
 $data = ['Tejash', 'Rajesh'];
 $info = [35, 40];
 mulArray.push($data, $info);
 console.log(mulArray);

See the output.

Javascript multidimensional array push

There are several methods for adding new elements to a JavaScript array. If you instead want to remove elements from an array in JavaScript, we’ve got an article for that too.

See the following methods.

  1. Using js array push() to add to the end of an array
  2. Using js array unshift() to add to the beginning of an array
  3. Using js array splice() to add elements within an array
  4. Using js array concat() to combine and create new, longer arrays

#Add an element at specified an index

We can also directly manipulate an array, we will not use any array methods, by referring to the index position within the array.

Here we add two new elements at specified positions. See the following code example.

// app.js
let list = ["foo", "bar"];
list[2] = "baz";
list[3] = "qux";
console.log(list)

See the following output.

Add an element at specified an index

#Add new array elements at the beginning of an array in Javascript

Use unshift. It’s like push, except it adds elements to the beginning of the array instead of the end.

  1. unshift/push: add an element to the beginning/end of an array.
  2. shift/pop: remove and return the first/last element of an array.

A simple diagram is following,

   unshift -> array <- push
   shift   <- array -> pop

and the chart is following.

          add  remove  start  end
   push    X                   X
    pop           X            X
unshift    X             X
  shift           X      X

Finally, Javascript Array Push Example | Array.prototype.push() Tutorial is over. Thanks for taking.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.