数组的操作方法

1.length 长度
2.push() 尾部添加
3.pop() 尾部删除最后一个元素
4.unshift() 开头添加一个元素
5.shift() 开头删除一个元素
6.concat() 合并两个或更多数组(元素)
var a = [1, 2]
var b = [3, [4]]
var c = 5
let arr = a.concat(b, c)
console.log(arr) // [ 1, 2, 3, [ 4 ], 5 ]
7.join() 把数组所有元素用传入的字符串分割
8.valueof() 返回数组的原始值
9.toString() 格式改为字符串
10.sort()按照定义的方法排序
var arr = [3, 2, 1, 4];
var arr1 = arr.sort(function (a, b) {
return a - b
});
console.log(arr1); //[1.2.3.4]
11.indexof() 返回给定参数的第一个索引,没找到返回-1
12.lastIndexOf() 返回给定参数的最后一个索引,没找到返回-1
13.splice( index,删除的长度 , 添加或替换的元素... )
可删除 添加 替换 但是前提是需要知道所操作位置的索引
var a = ['aa', 'bb', 'cc', 'dd']
a.splice(0, 0, 'AA', 'BB')
console.log(a) // ["AA", "BB", "aa", "bb", "cc", "dd"]

原文地址:https://www.cnblogs.com/vancissell/p/12934902.html