除了Object類型以外,Array類型恐怕是js中最經常使用的類型了,而且隨着js的發展進步,數組中提供的方法也愈來愈來,對數組的處理也出現了各類騷操做,此篇文章將會帶你從新學習數組中的實例方法前端
const arr = [1, 2, 3]
console.log(arr.join('|')) // '1|2|3'
複製代碼
const arr = [1, 2, 3]
console.log(arr.toString()) // '1,2,3'
複製代碼
let arr = ['a', 'b', 'c']
const count = arr.push('d')
console.log(count) // 4
console.log(arr) // [ 'a', 'b', 'c', 'd' ]
複製代碼
let arr = ['a', 'b', 'c']
const item = arr.pop()
console.log(item) // 'c'
console.log(arr) // [ 'a', 'b' ]
複製代碼
let arr = ['a', 'b', 'c']
const item = arr.shift()
console.log(item) // 'a'
console.log(arr) // [ 'b', 'c' ]
複製代碼
let arr = ['a', 'b', 'c']
const count = arr.unshift('d', 'e')
console.log(count) // 5
console.log(arr) // [ 'd', 'e', 'a', 'b', 'c' ]
複製代碼
let arr = ['a', 'b', 'c']
console.log(arr.reverse()) // [ 'c', 'b', 'a' ]
複製代碼
let arr = [2, 3, 10]
arr.sort()
console.log(arr) // [ 10, 2, 3 ]
複製代碼
'10'位於'2'以前
let arr = [2, 5, 3, 1]
arr.sort((a, b) => {
if (a < b) {
return -1
} else if (a > b) {
return 1
} else {
return 0
}
})
console.log(arr) // [ 1, 2, 3, 5 ]
複製代碼
let arr = [1, 2, 3]
let newArr = arr.concat(4, 5, [6, 7])
console.log(newArr) // [ 1, 2, 3, 4, 5, 6, 7 ]
複製代碼
let arr = [1, 2, 3, 4]
let newArr = arr.slice(1, 3)
console.log(newArr) // [ 2, 3 ]
複製代碼
tip: 若是slice方法的參數中有一個負數,則用數組長度加上該數來肯定相應的位置數組
let arr = [1, 2, 3, 4, 5]
arr.splice(0, 1)
console.log(arr) // [ 2, 3, 4, 5 ]
arr.splice(1, 0, 'hello', 'world')
console.log(arr) // [ 2, 'hello', 'world', 3, 4, 5 ]
arr.splice(3, 1, 'js')
console.log(arr) // [ 2, 'hello', 'world', 'js', 4, 5 ]
複製代碼
let arr = [1, 2, 3, 4, 5]
console.log(arr.indexOf(3)) // 2
console.log(arr.indexOf(3, 3)) // -1
複製代碼
一、every()方法對數組中每一項運行給定函數,若是該函數對每一項都返回true,則返回true
參數:callback(item, index, arr)函數
let arr = [3, 4, 5, 6]
let result = arr.every((item) => item > 2)
console.log(result) // true
複製代碼
let arr = [1, 2, 3, 4]
let result = arr.some((item) => item > 3)
console.log(result) // true
複製代碼
let arr = [1, 2, 3]
let result = arr.map(item => item * 2)
console.log(result) // [ 2, 4, 6 ]
複製代碼
let arr = [1, 2, 3, 4, 5]
let result = arr.filter(item => item > 2)
console.log(result) // [3, 4, 5]
複製代碼
let arr = [1, 2, 3, 4, 5]
arr.forEach(item => {
console.log(item) // 1 2 3 4 5
})
複製代碼
tip: 修改item的不會影響遍歷的數組項學習
let arr = [1, 2, 3, 4, 5]
let result = arr.reduce((accumulator, current, index, array) => {
return accumulator + current
})
console.log(result) // 15 1+2+3+4+5
複製代碼
let arr = [1, 2, 3, 4, 5]
let result = arr.reduce((accumulator, current, index, array) => {
return accumulator + current
}, 10)
console.log(result) // 25 10+1+2+3+4+5
複製代碼
let arrayLike = {
0: 1,
1: 2,
2: 3,
length: 3
}
console.log(Array.from(arrayLike)) // [1, 2, 3]
複製代碼
let arrayLike = {
0: 1,
1: 2,
2: 3,
length: 3
}
console.log(Array.from(arrayLike, (item) => item * 2)) // [2, 4, 6]
複製代碼
在實際應用中更多應用於Dom操做返回的集合以及函數內部的arguments對象ui
let result = Array.of(2, 3, 4)
console.log(result) // [ 2, 3, 4 ]
複製代碼
let arr = [1, 2, 3, 4, 5]
arr.copyWithin(0, 3, 5)
console.log(arr) // [ 4, 5, 3, 4, 5 ]
複製代碼
let arr = [1, 2, 3, 4, 5]
let result = arr.find(item => item > 3)
console.log(result) // 4
複製代碼
let arr = [1, 2, 3, 4, 5]
arr.fill(6, 2, 5)
console.log(arr) // [ 1, 2, 6, 6, 6 ]
複製代碼
let arr = ['a', 'b', 'c']
for (let idx of arr.keys()) {
console.log(idx) // 0 1 2
}
for (let item of arr.values()) {
console.log(item) // 'a' 'b' 'c'
}
for (let [idx, item] of arr.entries()) {
console.log(idx + '---' + item) // '0---a' '1---b' '2---c'
}
複製代碼
let arr = ['a', 'b', 'c']
console.log(arr.includes('a')) // true
console.log(arr.includes('d')) // false
複製代碼
let arr = ['a', 'b', ['c', 'd']]
let result = arr.flat() // ["a", "b", "c", "d"]
let arr1 = ['a', ['b', ['c']]]
//使用 Infinity 做爲深度,展開任意深度的嵌套數組
let result1 = arr1.flat(Infinity) // ["a", "b", "c"]
複製代碼
此篇文章記錄了JS中數組經常使用的方法,畢竟數組的實例方法有那麼多,好記性不如爛筆頭,寫下此篇文章加深記憶,同時但願對你們也能有所幫助。
若是有錯誤或不嚴謹的地方,歡迎批評指正,若是喜歡,歡迎點贊this