Article Directory
1 What is an array
Array objects use individual variable names to store a series of values.
An array can store all values with a variable name, and any value can be accessed with a variable name.
Each element in the array has its own id so that it can be easily accessed.
var arr=new Array();
1.1 Two ways to create an array
-
var arr=new Array(3)
arr[0]=“0”
arr[1]=“1”
arr[2]=“3” -
2.var arr=new Array( “xiaoming”, “xiaozhang”, "xiaoli“ )
1.2 A specific element can be accessed by specifying the array name or index
Access the element with index 0 in the array
1.3 Modify array elements
Modifying the value in an existing array
To modify the value in an existing array, just add a new value to the specified subscript number:
2 Array method
2.1 concat concatenates multiple arrays
concat:可以将两个数组拼接为一个数组
Note: The elements of the original array have not been changed, and the spliced array should be placed in the new array.
2.2 join puts array elements into a string
Two uses of join:
1.arr.join() 将数组的元素放到一个字符串中。
2.arr.join("-") 将数组以特殊字符分割后放入一个字符串中
2.3 pop ,push,unshift
The pop() method will delete the last element of arrayObject, reduce the length of the array by 1, and return the value of the deleted element. If the array is already empty, pop() does not change the array and returns the undefined value. (Change the value of the original array)
The push() method adds one or more elements to the end of the array and returns the new length. Change original length
The unshift() method adds one or more elements to the beginning of the array and returns the new length.
unshift is the opposite of push
2.4 reverse reverse the order of elements in an array
The reverse() method is used to reverse the order of elements in the array.
2.5 slice returns selected elements from the array
The slice() method can return selected elements from an existing array.
arrayObject.slice(start,end)
start: required . Specify where to start selection. If it is negative, it specifies the position from the end of the array. In other words, -1 refers to the last element, -2 refers to the penultimate element, and so on.
end: optional . Specify where to end the selection. This parameter is the array index at the end of the array fragment. If this parameter is not specified, the segmented array contains all elements from start to the end of the array. If this parameter is a negative number, then it specifies the elements counted from the end of the array. (Does not contain this element)
This method does not directly modify the array itself.
As shown in the figure below, the selected range of slices is **(included, not included)**.
2.6 splice delete specified elements
The splice() method adds/removes items to/from the array, and then returns the deleted items.
arrayObject.splice(index,howmany,item1,.....,itemX
index: required. Integer, which specifies the position of adding/deleting items. Use negative numbers to specify the position from the end of the array.
howmany: required. The number of items to delete. If set to 0, the item will not be deleted.
item: optional. New items added to the array.
The item element in splice replaces the deleted element in the original array.