The examples in this article summarize the methods of adding and deleting elements to JavaScript array Array objects. Share it with everyone 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.
Description
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.
Description
The shift method removes the first element from an array and returns that element.
Adds a new element to an array and returns the array's new length value.
arrayObj.push([item1 [item2 [. . . [itemN ]]]])
Parameters
arrayObj
Required. An Array object.
item, item2,. . . itemN
Optional. The new element of this Array.
Description
The push method will add new elements in the order they appear. If one of the arguments is an array, the array is added to the array as a single element. If you want to combine elements from two or more arrays, use the concat method.
splice method
Removes one or more elements from an array, inserts a new element at the position of the removed element if necessary, and returns the removed element.
arrayObj.splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])
Parameters
arrayObj
Required. An Array object.
start
Required. Specifies the starting position to remove elements from the array, starting from 0.
deleteCount
Required. The number of elements to remove.
item1, item2,. . .,itemN
Required. A new element to be inserted at the location of the removed element.
Description
The splice method modifies arrayObj by removing a specified number of elements starting from the start position and inserting new elements. The return value is a new Array object consisting of the removed elements.
concat method (Array)
Returns a new array that is a combination of two or more arrays.
array1.concat([item1[, item2[, . . . [, itemN]]]])
Parameters
array1
Required. The Array object to which all other arrays are to be concatenated.
item1,. . ., itemN
Optional. The other item to be connected to the end of array1.
Description
The concat method returns an Array object containing the concatenation of array1 and any other items provided.
The items to be added (item1...itemN) will be 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, it is added to the end of the array as a single array element.
The following is copying elements from the source array to the result array:
Object parameters copied from an array being concatenated to a new array still point to the same object after copying. No matter which one of the new array and the source array is changed, it will cause the other to change.
Only the value of a numeric or string concatenated into a new array is copied. Changing the values in one array does not affect the values in the other array.
I hope this article will be helpful to everyone’s JavaScript programming design.