Adding elements to the array
The push () method can add one or more array items to the end of the list:
var arr = [];arr.push (' A ', ' B '); Console.log (arr); [' A ', ' B ']
The Unshift () method adds one or more array items to the front of the arrays:
var arr = [' A ', ' B '];arr.unshift (n); Console.log (arr); [1, 2, "a", "B"]
The splice () method adds an array entry to the array:
var arr = [' A ', ' B ', ' C ', 1,2];arr.splice (2,0, ' d ', ' C ', ' e '); Console.log (arr); ["A", "B", "D", "C", "E", "C", 1, 2]
The Concat () method can add array items to an array, except that using this method does not alter the original array, creating a new array in the original array:
var arr = [' A ', ' B ', ' C '];var arr2 = arr.concat (' d ', 1,2,[' E ', 3]); Console.log (arr); ["A", "B", "C"]console.log (ARR2); ["A", "B", "C", "D", 1, 2, "E", 3]
The push () method adds one or more elements to the end of the array and returns the new length.
Grammar
Arrayobject.push (Newelement1,newelement2,...., newelementx)
Parameter description
Newelement1 required. The first element to add to the array.
Newelement2 is optional. The second element to add to the array.
Newelementx is optional. You can add multiple elements.
return value
Adds the specified value to the new length after the array.
Description
The push () method adds its parameter order to the tail of the arrayobject. It modifies the arrayobject directly, rather than creating a new array. The push () method and the Pop () method use the advanced post-stack functionality provided by the array.
Hints and Notes
Note: This method will change the length of the array.
Tip: To add one or more elements to the beginning of an array, use the Unshift () method.
Example: We will create an array and change its length by adding an element:
The code is as follows:
var arr = new Array (3);
Arr[0] = "George";
ARR[1] = "John";
ARR[2] = "Thomas";
document.write (arr + "") document.write (Arr.push ("James") + "") document.write (arr);
Output:
George,john,thomas
4
George,john,thomas,james
One usage on the Web:
The code is as follows:
$ (function () {
var buf = [];
Buf.push (' <script async src= ' http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js ' ></script > ');
Buf.push (' <ins class= ' Adsbygoogle "style=" display:inline-block;width:728px;height:90px "data-ad-client=" ca-pub-88888888 "data-ad-slot=" 8305246055 "></ins>");
Buf.push (' <script> (adsbygoogle = Window.adsbygoogle | | []). push ({});</script> ');
$ ('. Footer-banner '). HTML (Buf.join ("));
Note: You need to load the jquery.js first when using
Deleting an array to add an element
For array operations, in addition to adding array items, it is often necessary to delete a group. The usual methods for deleting array items are pop () and Shift () two methods.
Pop ()
The Pop () method can delete an array item from the end of the array:
var arr = [' A ', ' B ', ' C ', ' d ', 1,2];arr.pop (); Console.log (arr); ["A", "B", "C", "D", 1]
The shift () method is just the opposite of the pop () method, which deletes the first item of the array:
var arr = [' A ', ' B ', ' C ', ' d ', 1,2];arr.shift (); Console.log (arr); ["B", "C", "D", 1, 2]
Either the pop () or the shift () method can only delete an array entry for the array at a time, but many times it is relatively cumbersome to delete an array item. In the operation of an array, in addition to these two methods, you can also delete an array item by using the slice () and splice () methods.
The slice () method can remove multiple array items from an array, except that slice () does not affect the original array, but creates an array copy on the original array:
var arr = [1,2,3,4, ' A ', ' B '];var arr2 = Arr.slice (2); Console.log (arr); [1, 2, 3, 4, "A", "B"]console.log (ARR2); [3, 4, "A", "B"]console.log (ARR3); ["A", "B"]
The splice () method can also delete array entries for an array, in addition to adding array entries to the array:
var arr = [1,2,3,4, ' A ', ' B ', ' C '];var arr2 = Arr.splice (2,2); Console.log (arr); [1, 2, "a", "B", "C"]console.log (ARR2); [3, 4]
Changing elements in an array
The splice () method in an array is a powerful method in an array that, in addition to adding array items to an array, deleting array items, can also change an array:
var arr = [1,2,3,4,5,6];var arr2 = Arr.splice (2,3, ' a ', ' B ', ' C '); Console.log (arr); [1, 2, "a", "B", "C", 6]console.log (ARR2); [3, 4, 5]
Querying arrays
The array query mentioned here actually refers to the query extraction of the array. The method used is the slice () method:
var arr = [1,2,3,4,5,6];var arr2 = Arr.slice ( -3); Console.log (arr); [1, 2, 3, 4, 5, 6]console.log (ARR2); [4, 5, 6]
Summarize
Here is a simple collation of an array of the increment, delete, change, check the relevant methods. A simple summary:
Add Array entry methods: In addition to changing the value of an array item directly and modifying the length of the array to add an array entry method to an array, you can also use push (), Unshift (), concat (), and Splice ().
Delete Array Entry method: Delete Array Item method has pop (), Shift (), slice (), and Splice () method
Change Array Item Method: Change array item mainly by splice () method in array
Query Array Item method: Query Array Item method is actually the log group to do the query extraction function, the main use method is the slice () method
You can click here for information about the pop (), push (), shift (), and Unshift () methods, as described in Concat (), slice (), and Splice ().
The increment, delete, change and check of the array