數組操作方法
設置數組的長度
length
let abc = [1, 2, 3, 4, 5, 6, 7]
console.log(abc)
// (7) [1, 2, 3, 4, 5, 6, 7]
abc.length = 3
console.log(abc)
// (3) [1, 2, 3]
在數組末尾加一個/多個元素
push
// 在數組末尾加一個元素
let myPet = ['蘿卜', '荔枝', '來福']
myPet.push('旺財')
console.log(myPet, myPet.length)
// (4) ["蘿卜", "荔枝", "來福", "旺財"] 4
// 在數組末尾加多個元素
let myPet2 = ['蘿卜', '荔枝', '來福']
myPet2.push('旺財', '花花')
console.log(myPet2, myPet2.length)
// (5) ["蘿卜", "荔枝", "來福", "旺財", "花花"] 5
在數組末尾刪除一個元素,並返回這個被刪除元素
pop
let week = ['周一', '周二', '周三']
let week2 = week.pop()
console.log(week, week2)
// (2) ["周一", "周二"] "周三"
在數組頭部加一個/多個元素
unshift
// 在數組開頭加一個元素
let myPet3 = ['蘿卜', '荔枝', '來福']
myPet3.unshift('旺財')
console.log(myPet3, myPet3.length)
// (4) ["旺財", "蘿卜", "荔枝", "來福"] 4
// 在數組開頭加多個元素
let myPet4 = ['蘿卜', '荔枝', '來福']
myPet4.unshift('旺財', '花花')
console.log(myPet4, myPet4.length)
// (5) ["旺財", "花花", "蘿卜", "荔枝", "來福"] 5
在數組頭部刪除一個元素,並返回這個被刪除元素
shift
let week = ['周一', '周二', '周三']
let week2 = week.shift()
console.log(week, week2)
(2) ["周二", "周三"] "周一"
刪除數組任意位置且任意數量的元素
splice
arr.splice(index, n), 從第index個元素開始,刪除n個元素,並返回這個被刪除元素
let list = ['a', 'b', 'c', 'd', 'e']
let _list = list.splice(2, 1)
let list2 = ['a', 'b', 'c', 'd', 'e']
let _list2 = list2.splice(0, 1)
console.log(list, _list, list2, _list2)
// (4) ["a", "b", "d", "e"] ["c"] (4) ["b", "c", "d", "e"] ["a"]
檢查數組是否包含指定的值
includes
let rule = ['abc', 'edf']
arr.includes(rule)
將數組用value連接為字符串(不改變原數組)
join
let a = [1, 2, 3, 4, 5]
let res = a.join(',')
console.log(res) // '1,2,3,4,5'
console.log(a) // [1, 2, 3, 4, 5]
反轉數組(改變原數組)
reverse
let a = [1, 2, 3, 4, 5]
let res = a.reverse()
console.log(res) // [5, 4, 3, 2, 1]
console.log(a) // [5, 4, 3, 2, 1]
截取指定位置的數組()(不改變原數組)
slice(start, end)
// Base
let a = [1, 2, 3, 4, 5]
let result = a.slice(2, 4)
console.log(result) // [3, 4]
console.log(a) // [1, 2, 3, 4, 5]
// More
console.log(a.slice(1)) // [2, 3, 4, 5] 只有一個參數且不小於0,則從此索引開始截取到數組的末位
console.log(a.slice(-1)) // [5] 只有一個參數且小於0,則從倒數|start|位截取到數組的末位
console.log(a.slice(-1, 1)) // [] 反向截取,不合法返回空數組
console.log(a.slice(1, -1)) // [2, 3, 4] 從第一位截取到倒數第一位,不包含倒數第一位
console.log(a.slice(-1, -2)) // [] 反向截取,不合法返回空數組
console.log(a.slice(-2, -1)) // [4] 倒數第二位截取到倒數第一位
對數組元素進行排序(改變原數組)
sort
let a = [31, 22, 27, 1, 9]
let result = a.sort()
console.log(result) // [1, 22, 27, 31, 9]
console.log(a) // [1, 22, 27, 31, 9]
將數組中的元素用逗號拼接成字符串(不改變原數組)
toString
// Base
let a = [1, 2, 3, 4, 5]
let result = a.toString()
console.log(result) // 1,2,3,4,5
console.log(a) // [1, 2, 3, 4, 5]
從索引為0開始,檢查數組是否包含value,有則返回匹配到的第一個索引,沒有返回-1(不改變原數組)
indexOf
let a = [1, 2, 3, 2, 5]
let result = a.indexOf(2)
console.log(result) // 1
console.log(a) // [1, 2, 3, 2, 5]
從最后的索引開始,檢查數組是否包含value,有則返回匹配到的第一個索引,沒有返回-1(不改變原數組)
lastIndexOf
let a = [1, 2, 3, 2, 5]
let result = a.lastIndexOf(2)
console.log(result) // 3
console.log(a) // [1, 2, 3, 2, 5]
將數組和/或值連接成新數組(不改變原數組)
concat
// Base
let a = [1, 2], b = [3, 4], c = 5
let result = a.concat(b, c)
console.log(result) // [1, 2, 3, 4, 5]
console.log(a) // [1, 2]
// More
b = [3, [4]]
result = a.concat(b, c)
console.log(result) // [1, 2, 3, [4], 5] concat對於嵌套數組無法拉平
console.log(a) // [1, 2]
