1. Array.push() 在原數(shù)組末尾追加一個(gè)或多個(gè)元素,返回該數(shù)組的長(zhǎng)度
let arr = [ 1 , 2 , 3 ]
arr.push( 4 , 5 ) // 返回值為數(shù)組的長(zhǎng)度 5
console.log(arr) // [1, 2, 3, 4, 5]
2. Array.pop() 刪除數(shù)組的最后一個(gè)元素,并返回該元素
let arr = [ 1 , 2 , 3 , 4 ]
arr.pop() // 返回值為刪除的元素 4
console.log(arr) // [1, 2, 3]
3. Array.unshift() 在原數(shù)組前邊添加一個(gè)或多個(gè)元素,返回該數(shù)組的長(zhǎng)度
let arr = [ 1 , 2 , 3 ]
arr.unshift( 4 , 5 ) // 返回值為數(shù)組的長(zhǎng)度 5
console.log(arr) // [4, 5, 1, 2, 3]
4. Array.shift() 刪除數(shù)組的第一個(gè)元素,并返回該元素
let arr = [ 4 , 5 , 1 , 2 , 3 ]
arr.shift() // 返回值為刪除的元素 4
console.log(arr) // [5, 1, 2, 3]
5. Array.splice() 從原數(shù)組某個(gè)位置刪除/添加元素,返回刪除的元素?cái)?shù)組
let arr = [ 4 , 5 , 1 , 2 , 3 ]
arr.splice( 0 , 1 ) // 從 0 的位置,刪除一個(gè)元素,返回值為刪除的元素?cái)?shù)組 [4]
console.log(arr) // [5, 1, 2, 3]
arr.splice( 0 , 1 , 11 ) // 從 0 的位置,刪除一個(gè)元素,并添加一個(gè) 11 元素 返回值為刪除的元素?cái)?shù)組 [5]
console.log(arr) // [11, 1, 2, 3]
6. Array.slice() 返回選定的元素?cái)?shù)組,原數(shù)組不會(huì)改變
let arr = ['a','b','c','d']
arr.slice( 1 , 3 ) // 從索引為 1 的位置,取到 3 的位置,但不包含 3 ,返回值為 新數(shù)組 ['b', 'c']
console.log(arr) // ['a', 'b', 'c', 'd'] 原數(shù)組不會(huì)改變
7. Array.join() 使用某個(gè)拼接符,將數(shù)組轉(zhuǎn)化為字符串,返回該字符串,原數(shù)組不會(huì)改變
let arr = [ 1 , 2 , 3 , 4 , 5 ]
arr.join('-') // 返回拼接后的字符串 '1-2-3-4-5'
console.log(arr) // [1, 2, 3, 4, 5] 原數(shù)組不會(huì)改變
8. Array.forEach() 遍歷數(shù)組,為每一個(gè)元素調(diào)用一次函數(shù)
let arr = ['a','b','c']
arr.forEach((item, index, arr) => {
console.log(item) // 當(dāng)前遍歷元素項(xiàng)
console.log(index) // 當(dāng)前遍歷元素的索引
console.log(arr) // 原數(shù)組
})
9. Array.map() 遍歷數(shù)組,為每一個(gè)元素調(diào)用一次函數(shù),根據(jù)函數(shù)return返回的結(jié)果組成一個(gè)新的數(shù)組
let arr = ['a','b','c']
const newArr = arr.map((item, index, arr) => {
console.log(item, index, arr) // 元素項(xiàng),索引,當(dāng)前數(shù)組
return item += 2
})
console.log(arr) // 原數(shù)組不會(huì)改變
console.log(newArr) // 返回一個(gè)處理過的新數(shù)組 ['a2', 'b2', 'c2']
10. Array.filter() 遍歷數(shù)組,根據(jù)篩選出的符合條件的元素,組成一個(gè)新的數(shù)組
let arr = [ 1 , 2 , 3 , 4 ]
const newArr = arr.filter((item,index,arr) => {
return item > 2 // 返回元素項(xiàng)大于 2 的元素,組成一個(gè)新的數(shù)組
})
console.log(arr) // [1,2,3,4] 不會(huì)改變?cè)亟M
console.log(newArr) // [3, 4]
11. Array.find() 遍歷數(shù)組,返回第一個(gè)通過測(cè)試的元素項(xiàng)
let arr = [ 1 , 2 , 3 , 2 , 4 ]
const num = arr.find((item, index) => {
console.log(item, index) // index 到 1 的位置就不會(huì)在打印了,循環(huán)結(jié)束
return item >= 2 // 返回item >= 2的第一個(gè)元素項(xiàng),找到之后結(jié)束遍歷,不會(huì)繼續(xù)
})
console.log(arr) // [1,2,3,2,4] 不會(huì)改變?cè)亟M
console.log(num) // 2
12. Array.findIndex() 遍歷數(shù)組,返回第一個(gè)通過測(cè)試的元素的索引值
let arr = [ 1 , 2 , 3 , 2 , 4 ]
const num = arr.findIndex((item, index) => {
console.log(item, index) // index 到 1 的位置就不會(huì)在打印了,循環(huán)結(jié)束
return item >= 2 // 返回item >= 2的第一個(gè)元素項(xiàng)的索引值,找到之后結(jié)束遍歷,不會(huì)繼續(xù)
})
console.log(arr) // [1,2,3,2,4] 不會(huì)改變?cè)亟M
console.log(num) // 索引值為 1
13. Array.indexOf() 數(shù)組中是否存在某個(gè)元素,存在返回該索引,不存在返回-1,出現(xiàn)多次,也只會(huì)返回第一次出現(xiàn)時(shí)的索引
let arr = ['a','b','c','d', 'c', 'c']
arr.indexOf('c') // 返回值為 索引 2
arr.indexOf('ff') // 返回值為 -1 ff不存在數(shù)組中
14. Array.reduce() 遍歷數(shù)組,將函數(shù)的返回值,存儲(chǔ)到累加器中
let arr = [ 1 , 2 , 3 , 4 , 5 ]
const total = arr.reduce((total, item) => {
// 每次遍歷將total + item,下一次的遍歷時(shí),total為上一次返回的結(jié)果
console.log(total) // 1 3 6 10
return total + item
})
console.log(total) // 15
15. Array.from() 將具有l(wèi)ength屬性或者可迭代的對(duì)象轉(zhuǎn)化為數(shù)組
Array.from('abcdef') // 返回值['a', 'b', 'c', 'd', 'e', 'f']
Array.from(new Map([['b', 1 ], ['a', 2 ]]))
Array.from(new Set([ 1 , 2 , 3 ]))
本文轉(zhuǎn)自:辰漪博客