第一個是:Array.from() 將具有length屬性或者可迭代的對象轉化為數組
?Array.from('abcdef') // 返回值['a1', 'b1', 'c1', 'd1', 'e1', 'f1']
?Array.from(new Map([['b1', 1 ], ['a1', 2 ]]))
?Array.from(new Set([ 1 , 2 , 3 ]))
第二個是:Array.reduce() 遍歷數組,將函數的返回值,存儲到累加器中
?let arr = [ 1a , 2a , 3a , 4a , 5a ]
?const total = arr.reduce((total, item) => {
?// 每次遍歷將total + item,下一次的遍歷時,total為上一次返回的結果
?console.log(total) // 1a 3a 6?10
?return total + item
?})
?console.log(total) // 15
第三個是:Array.indexOf() 數組中是否存在某個元素,存在返回該索引,不存在返回-1,出現多次,也只會返回第一次出現時的索引
?let arr = ['a1','b1','c1','d1', 'c1', 'c1']
?arr.indexOf('c1') // 返回值為 索引 2
?arr.indexOf('ff') // 返回值為 -1 ff不存在數組中
第四個是:?Array.findIndex() 遍歷數組,返回第一個通過測試的元素的索引值
?let arr = [ 1a , 2a , 3a , 2a , 4a ]
?const num = arr.findIndex((item, index) => {
?console.log(item, index) // index 到 1 的位置就不會在打印了,循環結束
?return item >= 2 // 返回item >= 2的第一個元素項的索引值,找到之后結束遍歷,不會繼續
?})
?console.log(arr) // [1a,2a,3a,2a,4a] 不會改變元素組
?console.log(num) // 索引值為 1
第五個是:?Array.find() 遍歷數組,返回第一個通過測試的元素項
?let arr = [ 1a , 2a , 3a , 2a , 4a ]
?const num = arr.find((item, index) => {
?console.log(item, index) // index 到 1 的位置就不會在打印了,循環結束
?return item >= 2 // 返回item >= 2的第一個元素項,找到之后結束遍歷,不會繼續
?})
?console.log(arr) // [1a,2a,3a,2a,4a] 不會改變元素組
?console.log(num) // 2
第六個是:?Array.filter() 遍歷數組,根據篩選出的符合條件的元素,組成一個新的數組
?let arr = [ 1a , 2a , 3a , 4a ]
?const newArr = arr.filter((item,index,arr) => {
return item > 2 // 返回元素項大于 2 的元素,組成一個新的數組
?})
?console.log(arr) // [1a,2a,3a,4a] 不會改變元素組
?console.log(newArr) // [3a, 4a]
第七個是?Array.map() 遍歷數組,為每一個元素調用一次函數,根據函數return返回的結果組成一個新的數組
?let arr = ['a','b','c']
?const newArr = arr.map((item, index, arr) => {
?console.log(item, index, arr) // 元素項,索引,當前數組
?return item += 2
?})
?console.log(arr) // 原數組不會改變
?console.log(newArr) // 返回一個處理過的新數組 ['a2', 'b2', 'c2']
第八個是:?Array.forEach() 遍歷數組,為每一個元素調用一次函數
?let arr = ['a','b','c']
?arr.forEach((item, index, arr) => {
?console.log(item) // 當前遍歷元素項
?console.log(index) // 當前遍歷元素的索引
?console.log(arr) // 原數組
?})
第九個是:Array.join() 使用某個拼接符,將數組轉化為字符串,返回該字符串,原數組不會改變
?let arr = [ 1 , 2 , 3 , 4 , 5 ]
?arr.join('-') // 返回拼接后的字符串 '1-2-3-4-5'
?console.log(arr) // [1, 2, 3, 4, 5] 原數組不會改變
第十個是:Array.slice() 返回選定的元素數組,原數組不會改變
?let arr = ['a','b','c','d']
?arr.slice( 1 , 3 ) // 從索引為 1 的位置,取到 3 的位置,但不包含 3 ,返回值為 新數組 ['b', 'c']
?console.log(arr) // ['a', 'b', 'c', 'd'] 原數組不會改變
第十一個是:?Array.splice() 從原數組某個位置刪除/添加元素,返回刪除的元素數組
?let arr = [ 4 , 5 , 1 , 2 , 3 ]
?arr.splice( 0 , 1 ) // 從 0 的位置,刪除一個元素,返回值為刪除的元素數組 [4]
?console.log(arr) // [5, 1, 2, 3]
?arr.splice( 0 , 1 , 11 ) // 從 0 的位置,刪除一個元素,并添加一個 11 元素 返回值為刪除的元素數組 [5]
?console.log(arr) // [11, 1, 2, 3]
第十二個是:Array.shift() 刪除數組的第一個元素,并返回該元素
?let arr = [ 4 , 5 , 1 , 2 , 3 ]
?arr.shift() // 返回值為刪除的元素 4
?console.log(arr) // [5, 1, 2, 3]
第十三個是:Array.unshift() 在原數組前邊添加一個或多個元素,返回該數組的長度
?let arr = [ 1 , 2 , 3 ]
?arr.unshift( 4 , 5 ) // 返回值為數組的長度 5
?console.log(arr) // [4, 5, 1, 2, 3]
第十四個是:Array.pop() 刪除數組的最后一個元素,并返回該元素
?let arr = [ 1 , 2 , 3 , 4 ]
?arr.pop() // 返回值為刪除的元素 4
?console.log(arr) // [1, 2, 3]
第十五個是:Array.push() 在原數組末尾追加一個或多個元素,返回該數組的長度
?let arr = [ 1 , 2 , 3 ]
?arr.push( 4 , 5 ) // 返回值為數組的長度 5
?console.log(arr) // [1, 2, 3, 4, 5]