| api | 改变原数组 | 不改变原数组 |
|---|---|---|
| length | √ | |
| concat | √ | |
| push | √ | |
| unshift | √ | |
| pop | √ | |
| shift | √ | |
| slice | √ | |
| splice | √ | |
| reverse | √ | |
| sort | √ | |
| flat | √ | |
| indexOf | √ | |
| lastIndexOf | √ | |
| every | √ | |
| fill | √ | |
| filter | √ | |
| join | √ | |
| forEach | √ | |
| map | √ | |
| reduce | √ |
length
简介:查询数组属性长度
[1,2,3,3].length // 4
concat
简介:合并一个或多个数组
array.concat(...item)
[1,2,3].concat([4,5,6],[7,8,9]) //[1,2,...,9]
push
简介:向原数组后添加一个或多个数组元素
array.push(...item)
[1,2,3].push('a') //[1,2,3,'a']
unshift
简介:向原数组头部添加一个或多个数组元素
array.unshift(...item)
[1,2,3].unshift('a') //['a',1,2,3]
pop
简介:向原数组删除最后一个数组元素,返回被删除元素值
array.pop()
[1,2,3].pop() //3
shift
简介:删除原数组下标为0的元素,返回被删元素值
array.shift()
[1,2,3].shift()
slice
简介:根据下标截取拷贝数组
array.slice(start,?end)
[1,2,3,4].slice(1) //[2,3,4]
[1,2,3,4].slice(1,3) //[2,3]
splice
简介: 删除指定区间,并插入新元素,返回被删除元素
array.splice(start,deleteCount[,item])
let a = [1,2,3,4]
a.splice(0,2,'a') //[1,2]
//a => ['a',3,4]
reverse
简介:翻转数组
array.reverse()
[1,2,3,4].reverse() // [4,3,2,1]
sort
简介:数组排序
array.sort((a,b)=>a-b)
array.sort((a,b)=>b-a)
[1,3,5,1,2,5].sort((a,b)=>a-b) //[ 1, 1, 2, 3, 5, 5 ]
[1,3,5,1,2,5].sort((a,b)=>b-a) //[ 5, 5, 3, 2, 1, 1 ]
flat
简介:数组扁平化,Infinity不论多少层,全部扁平
array.flat( |number|Infinity)
[1,2,3,[4,5,[6,7,[8,9]]]].flat() // [1, 2, 3, 4, 5,[6,7,[8,9]]]
[1,2,3,[4,5,[6,7,[8,9]]]].flat(2) // [1, 2, 3, 4, 5,6,7,[8,9]]
[1,2,3,[4,5,[6,7,[8,9]]]].flat(Infinity) // [1, 2, 3, 4, 5,6,7,8,9]
includes
简介: 元素是否存在数组,存在返回true,不存在返回false
array.includes(item)
[1,2,3].includes(1) //true
[1,2,3].includes('a') // false
indexOf
简介: 从开始搜索元素是否存在,存在返回下标,不存在返回-1
array.indexOf(item)
[1,2,3].indexOf(1) // 0
[1,2,3].indexOf(4) // -1
lastIndexOf
简介: 从尾部开始搜索元素是否存在,存在返回下标,不存在返回-1
array.lastIndexOf(item)
[1,2,3].lastIndexOf(1) // 0
[1,2,3].lastIndexOf(4) // -1
every
简介: 检测数组所有元素是否都符合指定条件
array.every(function(item,index),?thisArg)
[1, 2, 3, 4, 1, 12, 1].every(item=>item>=1) //true
[1, 2, 3, 4, 1, 12, 1].every(item=>item>1) //false
fill
简介: es6添加,对数组进行填充
array.fill(value,?start, ?end)
let a = [1,2,4,3,1,3,4,5,6]
a.fill(3,2) // [1,2,3,3,3,3,3,3,3]
a //[1,2,3,3,3,3,3,3,3]
filter
简介: 筛选出所有符合条件的数组项
array.filter(function(value),?thisArg)
let a = [1, 2, 4, 3, 1, 3, 4, 5, 6]
a.filter(item=>item>3) // [4,4,5,6]
join
简介: 按照传入的字符串拼接数组的每一项,默认逗号
array.join(?separator)
let a = ['hello', 'world']
a.join(' ') // hello world
forEach
简介:遍历数组每一项
array.forEach(function(item,index,arr),?thisAsg)
[1,2,3,'hello'].forEach((item,index,arr)=>console.log(item,index,arr))
// 1 0 [1,2,3,'hello']
// 2 1 [1,2,3,'hello']
// 3 2 [1,2,3,'hello']
map
简介:遍历数组每一项
array.map(function(item,index,arr),?thisAsg)
[1,2,3,'hello'].map((item,index,arr)=>item+1)
//[2,3,4,'hello']
reduce
简介:累加器
array.reduce(function(total,item,index,arr),?thisArg)
[1,2,3,4].reduce((total,item)=>total+item) // 10