This example summarizes the methods of adding and deleting elements of the JavaScript array Array object. Share it for your reference. The specific analysis is as follows:
pop method
Removes the last element in the array and returns that element.
arrayObj.pop( )
The required arrayObj reference is an Array object.
illustrate
If the array is empty, undefined will be returned.
shift method
Removes the first element in the array and returns that element.
arrayObj.shift( )
The required arrayObj reference is an Array object.
illustrate
The shift method removes the first element in the array and returns that element.
The code copy is as follows: var arr = new Array(0,1,2,3,4);
var remove = arr.pop();
alert(remove);
alert(arr.length);
Remove and return the last element, pop up 4 first, and then prompt that the current array length pops up 4!
push method
Adds a new element to an array and returns the new length value of the array.
arrayObj.push([item1 [item2 [. . . [itemN ]]]])
parameter
arrayObj
Required option. An Array object.
item, item2,. . itemN
Optional. The new element of the Array.
illustrate
The push method will add the new elements in the order in which they appear. If one of the parameters is an array, the array is added to the array as a single element. If you want to merge elements from two or more arrays, use the concat method.
The code copy is as follows: var arr = new Array(0,1,2,3,4);
// Parameters are one or more
var len = arr.push(5,6);
//len = arr.push(7);
for(var i=0;i<arr.length;i++){
alert(arr[i]);
}
You can add multiple in at one time, or you can add one to return the current length of the array. Changes in the print array content to observe changes!
splice method
Remove one or more elements from an array, and, if necessary, insert a new element at the removed element position, returning the removed element.
arrayObj.splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])
parameter
arrayObj
Required option. An Array object.
start
Required option. Specifies the starting position for removing elements from the array, which is calculated from 0.
deleteCount
Required option. The number of elements to be removed.
item1, item2,. . .,itemN
Required option. New element to be inserted at the location of the removed element.
illustrate
The splice method can remove the specified number of elements starting from the start position and insert new elements, thereby modifying arrayObj. The return value is a new Array object composed of the removed elements.
The code copy is as follows: var arr = new Array(0,1,2,3,4);
// Delete two elements starting from 2, and the position starts from 0
// Returns the array of elements removed
var reArr = arr.splice(2,2);
// You can replace new elements at the location where the element is removed
//Just add new elements from the starting position of the removal. If you remove two elements, you can add 10 new elements to enter
//var reArr = arr.splice(2,2,6,7,8,9);
for(var i=0;i<arr.length;i++){
alert(arr[i]);
}
If you don't want to add new elements, then don't pass the third parameter!
concat method (Array)
Returns a new array, which is composed of two or more arrays.
array1.concat([item1[, item2[, . . . [, itemN]]]])
parameter
array1
Required option. All other arrays are connected to Array objects.
item1,. . ., itemN
Optional. To connect to other items at the end of array1.
illustrate
The concat method returns an Array object containing the connection between array1 and any other items provided.
The items to be added (item1 … itemN) are added to the array in order from left to right. If an item is an array, add its contents to the end of array1. If the item is not an array, add it as a single array element to the end of the array.
The following is the copying element from the source array to the result array:
For object parameters copied from the array being connected to the new array, the copy still points to the same object. Regardless of which of the new array or the source array changes, it will cause the other to change.
For values or strings connected to the new array, copy only its values. Changes in values in one array do not affect the values in another array.
The code copy is as follows: var arr = new Array(0,1);
var arr2 = new Array(3,4);
var arr = arr.concat(arr2);
for(var i=0;i<arr.length;i++){
alert(arr[i]);
}
The purpose of the method is to copy the elements in arr2 into arr!
I hope this article will be helpful to everyone's JavaScript programming.