The push() function can append a number, string, object, Array, or any value to the Array.
Let us see the syntax of the array push() method.
array.push(element)You can use a push() method to appkết thúc more than one value to lớn an array in a single hotline.
array.push(item1, item2, item3,...,itemN)
It returns the Number representing the new length of the Array.
The push() method can be used with a call() or apply() on objects resembling the arrays. The array.push() method relies on the length property lớn resolve where to insert the given elements.
If the length cannot be converted into lớn a number, the index used is 0.
It includes the possibility of length being nonexistent, in which case the length will also be created.
// phầm mềm.jsconst songs = <'Dangerous', 'Thriller'>;const totalSongs = songs.push('Stranger In Moscow');console.log(totalSongs);console.log(songs);See the output.
➜ es git:(master) ✗ node app3< 'Dangerous', 'Thriller', 'Stranger In Moscow' >In this example, we have added the third element called “Stanger in Moscow“. After adding an cống phẩm khổng lồ the Array, we have sầu printed the mutated Array and its length.
Adding multiple items in the Array
If you have seen the syntax of the Array.push() method, và then you can see that we can add multiple elements as arguments at a single time.
Let us take an example of how we can merge two arrays.
// server.jsconst artists = <'michael', 'elvis'>;const newArtists = <'justin', 'charlie'>;Array.prototype.push.apply(artists, newArtists);console.log(artists);See the output.
➜ es git:(master) ✗ node app< 'michael', 'elvis', 'justin', 'charlie' >➜ es git:(master) ✗Do not use the apply() method if the second Array (newArtists in the example) is vast. The maximum number of arguments that one function can handle is limited in practice.
So, apply() will add the second Array inlớn the first Array, and we can see the combined Array by returning the original Array, and in our case, it is artists.
See the following code. We have taken an array of Strings.
// VPS.jsconst artists = <'michael', 'elvis'>;artists.push('justin', 'charlie');console.log(artists); // <'michael', 'elvis', 'justin', 'charlie'>Here, we have sầu added two more artists. Now, see the following code.
// hệ thống.jsconst artists = <'michael', 'elvis'>;const newArtists = <'justin', 'charlie'>;artists.push(newArtists);console.log(artists);See the output.
➜ es git:(master) ✗ node app< 'michael', 'elvis', < 'justin', 'charlie' > >➜ es git:(master) ✗Here, the third element is an array. So it has added the third sản phẩm as a whole array. So we can not combine it as we have sầu done it with apply().
Here, See the following code.
// app.jsarrObj = < name: 'Krunal', age: 26 , name: 'Ankit', age: 24 >;arrObj.push( name: 'Rushabh', age: 27);console.log(arrObj);See the following output.
Adding Array into the Array using push()
// phầm mềm.jsarrArray = < <'Krunal', 'Ankit'>, <'Rushabh', 'Dhaval'>>;arrArray.push(< 'Tejash', 'Rajesh'>);console.log(arrArray);See the output.
It does not add or appending lớn an existing array. It simply creates a copy và performs the operation in that copy, và returns that array.
Now, from the năm 2016 update: with spread, you don’t need that apply() method anymore.
See the following programming example.
// app.js mulArray = <>; $data = <'Tejash', 'Rajesh'>; $info = <35, 40>; mulArray.push($data, $info); console.log(mulArray);See the output.
See the following methods.
Adding an element lớn a specific position in the Array
To add an element to a specific position, you need to refer lớn its index position within the array.
Here we add two new elements at specified positions. See the following code example.
// ứng dụng.jslet list = <"foo", "bar">;list<2> = "baz";list<3> = "qux";console.log(list)See the following output.
Adding element at the beginning of the Array
Use unshift() method. It’s like push, except it inserts elements to the beginning of the Array instead of the end.
A simple diagram is following,
unshift -> array pop& the chart is the following.
add remove sầu start kết thúc push X X pop X Xunshift X X shift X X
A simple example, suppose you have sầu an array of the children of House Stark from Game of Thrones. However, one of the members, Bran who is missing, which is funny because it is the King of Six kingdoms. Using the push() method, you could add hyên ổn by assigning ‘Bran‘ khổng lồ the array at the index after the last index.