An array of space
Arrays can be created with a string of commas preceded by an item with the value undefined
const arr = [, , 2]
console.log(arr[1]) // undefined
console.log(arr[2]) // 2
Copy the code
———-
arr.length
Changing length applies directly to the array:
- Reducing length removes elements from the end of the array
- Increasing length adds an element of undefined to the end of the array
const arr = [0, 1, 2] console.log(arr.length) // 3 arr.length=2 console.log(arr) // [0,1] arr.length= 5 console.log(arr) // [0, 1, undefined undefined undefined]Copy the code
———-
Array.isArray(arr)
Array or not
Returns a Boolean value
console.log(1)
const arr = [0, 1, 2]
const x = 1
console.log(Array.isArray(arr)) // true
console.log(Array.isArray(x)) // false
Copy the code
———-
Array.from(pseudoarray, mapFn)
Converts a pseudo-array to a real array and returns a converted real array
const str = 'JT'
const arr = Array.from(str, v => v + 's')
console.log(arr) // [ 'Js', 'Ts' ]
Copy the code
arr.toString()
Convert an array with a comma to a string
const arr = ['a', 'b', 'c']
arr.toString()
console.log(arr.toString()) // a,b,c
console.log(arr) // [ 'a', 'b', 'c' ]
Copy the code
Arr. Join (splice)
Concatenates an array into a string using the specified concatenate
Concatenation does not specify the default use of commas
const arr = ['a', 'b', 'c']
console.log(arr.join()) // a,b,c
console.log(arr.join('-')) // a-b-c
console.log(arr) // [ 'a', 'b', 'c' ]
Copy the code
———-
arr.concat(arr2,arr3…)
Merges two or more arrays to return a new array
const arr = [0, 1, 2] const arr2 = [4, 5, 6] const arr3 = [8, 9] console.log(arr.concat(arr2, Arr3)) / /,1,2,4,5,6,8,9 [0] the console log (arr) / / [0, 1, 2]Copy the code
Arr. Slice (star, end)
Shallowly copies the original array from star subscript to end subscript (excluding end), returning a new array
Star and end are optional. If this parameter is not specified, copy the file to the end in the corresponding direction
End >star must be positive, otherwise an empty array is returned
const arr = [0, 1, 2, 3, 4, 5] console.log(arr.slice(2, 4)) // [ 2, 3 ] console.log(arr.slice(2, 5)) // [ 2, 3, 4 ] console.log(arr.slice(2, 6)) // [ 2, 3, 4, 5 ] console.log(arr.slice(5, 2)) // [] console.log(arr.slice(-1, 2)) / / []Copy the code
arr.flat(depth)
Expands multidimensional array at specified depth, returns a new array with no null values (automatically removes empty items)
const arr1 = [1, 2, [3, 4]]
arr1.flat()
console.log(arr1.flat()) // [ 1, 2, 3, 4 ]
console.log(arr1) // [1, 2, [3, 4]]
Copy the code
———-
The following methods change the original array:
Arr. Fill (fill, star, end)
Fills all elements of the array from star to end, excluding end, with a fixed value
Ensure that end>star and the value is positive. If star and end are greater than length-1, the length will not change
Const arr = [0, 1, 2, 3, 4, 5] Arr. Fill (8,2,7) console.log(arr) // [0, 1, 8, 8, 8, 8] const arr2 = [0, 1, 2, 3, 4, 5] arr. The fill,7,2 (8) the console. The log (arr2) / / [0, 1, 2, 3, 4, 5] const arr3 = [0, 1, 2, 3, 4, 5] arr. The fill (8, 1, 7). The console log (arr3) / / [0, 1, 2, 3, 4, 5]Copy the code
arr.push(data1,data2…)
Adds one or more elements to the end of an array and returns the new length of the array
Let arr = [0, 1, 2, 3, 4, 5] let l = arr. Push (6, 7, 8, 9) the console. The log (arr) / /,1,2,3,4,5,6,7,8,9 [0] the console log (l) / / 10Copy the code
arr.pop()
Removes the last element from the array and returns the value of that element
let arr = [0, 1, 2, 3, 4, 5]
let l = arr.pop()
console.log(arr) // [ 0, 1, 2, 3, 4 ]
console.log(l) // 5
Copy the code
arr.unshift(data1,data2…)
Adds one or more elements to the beginning of an array and returns the new length of the array
Unshift (0, 1,2) console.log(arr) // [0,1,2,3,4,5] console.log(l) // 6Copy the code
arr.shift()
Removes the first element from the array and returns the value of that element
let arr = [0, 1, 2, 3, 4, 5]
let l = arr.shift()
console.log(arr) // [ 1, 2, 3, 4, 5 ]
console.log(l) // 0
Copy the code
arr.reverse()
Invert the original array to return the reversed array (the original array will also change)
let arr = [0, 1, 2, 3, 4, 5]
let newArr = arr.reverse()
console.log(newArr) // [ 5, 4, 3, 2, 1, 0 ]
console.log(arr) // [ 5, 4, 3, 2, 1, 0 ]
Copy the code
Arr. Splice (star, number of deletions, add item 1, add item 2…)
Splice adds to, deletes from, and returns the modified content as an array
let arr = ['a', 'b', 'c', 'd', 'e', 'f']
let newArr = arr.splice(1, 4, '6', '7', '8')
console.log(arr) // [ 'a', '6', '7', '8', 'f' ]
console.log(newArr) // [ 'b', 'c', 'd', 'e' ]
Copy the code
Arr.sort ((a, b) => a-b or b-a)
No arguments are specified, and the sort result is determined by the implementation
You can specify that the argument is a callback function and that the elements are reordered according to the Unicode points of each character in the converted string
Return a-b, reordering the Unicode loci from smallest to largest
Return B-A, reordering the Unicode loci from largest to smallest
Let arr1 = [1,4,0,3,2] let newArr1 = arr1.sort((a,b)=>a-b) console.log(arr1) // [0, 1, 2, 3, 4 ] console.log(newArr1) // [ 0, 1, 2, 3, Sort ((a,b)=>b-a) console.log(arr2) // 0 ] console.log(newArr2) // [ 4, 3, 2, 1, 0 ]Copy the code
———-
ES5 new array method:
(Skip undefined)
arr.forEach((item, index) => {})
traverse
Return value is always undefined(even if you specify return)
let arr = [0, 1, 2, 3, 4]
let a = arr.forEach(item => 1)
console.log(a) // undefined
Copy the code
arr.map((item, index) => {})
Traverse & collect returned items
Return a new array
let arr = [0, 1, 2, 3, 4] let a = arr.map(item => item+1) console.log(arr) // [ 0, 1, 2, 3, 4 ] console.log(a) // [ 1, 2, 3, 4, 5]Copy the code
Arr. Filter ((item, index) =>
Filter & retain items that return true
Return a new array
let arr = [0, 1, 2, 3, 4]
let a = arr.filter(item => item>=2)
console.log(arr) // [ 0, 1, 2, 3, 4 ]
console.log(a) // [ 2, 3, 4 ]
Copy the code
Arr. Reduce ((sum, item, index) => sum + item, sum initial value)
Traversal & cumulative summation
Return cumulative results
let arr = [0, 1, 2, 3, 4]
let a = arr.reduce((sum, item, index) => sum + item, 0)
console.log(arr) // [ 0, 1, 2, 3, 4 ]
console.log(a) // 10
Copy the code
Arr. Every ((item, index) =>
Traverse & determine if all conditions are met
Returns a Boolean value
let arr = [0, 1, 2, 3, 4]
let a = arr.every((item, index) => item>1)
console.log(arr) // [ 0, 1, 2, 3, 4 ]
console.log(a) // false
Copy the code
Arr. Some ((item, index) =>
Traverse & determine if there is an item that satisfies the condition
Returns a Boolean value
let arr = [0, 1, 2, 3, 4]
let a = arr.some((item, index) => item>1)
console.log(arr) // [ 0, 1, 2, 3, 4 ]
console.log(a) // true
Copy the code
———-
ES6 add array method:
(Skip undefined)
Arr. Find ((v, I, arr)=>{function body})
Index and array are optional
Returns the value of the first element in the array that satisfies the provided test function, otherwise undefined
const arr = [0, 1, 2]
console.log(arr.find((v, i, arr) => v > 3)) // undefined
Copy the code
Arr. FindIndex ((v, I, arr)=>{function body})
Index and array are optional
Returns the index of the first element in the array that satisfies the provided test function, otherwise -1
const arr = [0, 1, 2]
console.log(arr.findIndex((v, i, arr) => v > 3)) // -1
Copy the code
———-
ES7 add array method:
Arr.includes (target, start subscript)
Whether the target is in the array
Returns a Boolean value
Search direction: head to tail
const arr = [0, 1, 2]
console.log(arr.includes(3)) // false
Copy the code
Arr.indexof (Target, start subscript)
The index of the element’s first occurrence (beginning to end)
Returns the index of the first occurrence of the target, -1 if not found
const arr = ['0', '1', '2']
console.log(arr.indexOf('0')) // 0
console.log(arr.indexOf('0', 1)) // -1
console.log(arr.indexOf('3')) // -1
Copy the code
Arr. LastIndexOf (Target, start subscript)
The first occurrence of an element (tail to head)
Returns the index of the first occurrence of the target, -1 if not found
Search direction: tail to head (search direction does not affect the subscript)
const arr = ['0', '1', '2']
console.log(arr.lastIndexOf('0')) // 0
console.log(arr.lastIndexOf('0', 2)) //0
console.log(arr.indexOf('3')) // -1
Copy the code
———-
Array deconstruction Assignment (new in ES6)
Extract values from an array and assign them to a set of variables according to a pattern
Structure assignment requires one-to-one correspondence, and unneeded values are skipped directly (comma reserved)
Let arr = [1, 2, 3, 4] // Fully destruct let [a, b, c, d] = arr console.log(a, b, c, d) // 1 2 3 4 // not fully destruct let [a, C, d] // d] = arr console.log(a, c, d) // 1 3 4Copy the code
———-
Expansion operator (…)
You can expand the items of an array
// Expand array let arr = [1, 3, 5, 7] console.log(... Let arr2 = [1, 2, 3] let arr2 = [4, 5, 6] let newArr = [...arr1, Arr2] console.log(newArr) // [1, 2, 3, 4, 5, 6] let arr3 = [3, 100, 20] let Max = math.max (... arr3) console.log(max) // 100Copy the code