常用的数组对象操作方法

数组操作方法
设置数组的长度

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]

原文地址:https://www.cnblogs.com/unclefang/p/11180379.html