The Array object is used to store multiple values in a single variable.
1. Create an array object
(1) Use literals to create an array
(2) Use the new keyword to create an array
2. Determine whether an object is an array
(1) Instanceof is an array if it returns true, false is a non-array
(2) Array.isArray() if it returns true is an array, and false is a non-array
(3) valueOf() returns the array object itself
3. Stack operation
Add at the end, delete at the end
push() 添加元素
pop() 删除元素
4. Queue operation
Add head, delete head
shift() 删除元素
unshift() 添加元素
5. Array flip and sort
(1) Flip the array, reverse()
(2) Sort, sort()
Huh? ? ? Are you full of question marks, haha!
If you use the sort method directly, the elements in the array will be sorted alphabetically. To be more precise,It is sorted according to the order of character encoding。
Therefore, we need to pass in a parameter in the sorting method. This parameter must be a function to specify the sorting rules we want.
6. Location method
indexOf() 返回元素第一次出现的索引值 没有返回 -1
lastIndexOf() 返回元素最后一次出现的索引值 没有返回-1
7、Join()
Convert the array to a string.
We can specify the separator between the elements, the default is comma separation
8. Combine arrays
concat(): concatenate the parameters to the current array, or connect two or more arrays
9, intercept the array
(1) slice(start,end) :
Intercept elements from start to end, including start, excluding end, and return a new array
(2)splice()
Intercept elements from start, intercept length, and return a new array, start is the index, length is the number, and the content intercepted in the original array will be deleted
10. Clear the array
//方式1 推荐
arr = []
//方式2
arr.length = 0
//方式3
arr.splice(0, arr.length)
11. Array iteration method
(1)foreach
- Used to call each element of the array and pass the element to the callback function
(2)every()和some()
- every(), check whether all elements of the array meet the specified conditions, if they are met, the return value is true, as long as one does not meet, it is false
- some(), to determine whether one of the expressions in the callback function is satisfied, if at least one is satisfied, the return value is true
(3) filter () and map ()
- filter() filters elements according to specified conditions and returns a new array;
- map() returns a new array based on mathematical operations