总结JS数组的一些常用的方法,便于开发中遗忘了查找使用方式,将会持续更新~
push() 和 pop()
push() 方法在数组
末尾添加一个元素,返回数组的长度
const arr = [1,3,5]
const len = a.push(7)
console.log(arr) => [1,3,5,7]
console.log(len) => 4
pop() 方法在数组
末尾删除一个元素,返回删除的元素
const arr = [1,3,5]
const item = a.pop()
console.log(arr) => [1,3]
console.log(item) => 5
unshift() 和 shift()
unshift() 方法在数组
开头添加一个元素,返回数组的长度
const arr = [1,3,5]
const len = a.unshift(7)
console.log(arr) => [7,1,3,5]
console.log(len) => 4
shift() 方法在数组
开头删除一个元素,返回删除的元素
const arr = [1,3,5]
const item = a.pop()
console.log(arr) => [3,5]
console.log(item) => 1
includes()
该方法用于判断数组中
是否存在某个元素
const arr = [1, 3, 5, 7, 9, 11]
arr.includes(1) => true
arr.includes(2) => false
forEach()
这是最常用的用来
遍历数组的方法,不会改变原数组,而且没有返回值
const arr = [1, 3, 5, 7, 9, 11]
arr.forEach(item => {
console.log(item)
})
=> 1,3,5,7,9,11
map()
也是常用来
遍历数组的方法,不会改变原数组,有返回值,该方法会返回一个新的数组,默认返回undefined
const arr = [1, 3, 5, 7, 9]
const newArr = arr.map(item => item + 1)
=> newArr = [2, 4, 6, 8, 10]
如果没有显式跟隐式返回值的话,得到的数组是所有元素都为 undefined 的数组
const newArr = arr.map((item, index)=> {console.log(item, index)})
=> newArr = [undefined, undefined, undefined, undefined, undefined]
filter()
这个方法是用来过滤的,该方法返回一个新数组,
不会改变原数组
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const newArr = arr.filter(item => item % 2 === 0)
=> newArr = [2, 4, 6, 8]
find()
根据检索条件,找到数组中
第一个满足条件的元素,若找不到则返回 undefined
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const newFind = arr.find(item => item % 2 === 0)
=> newFind = 2
findIndex()
该方法与 find() 类似根据检索条件,不同的是该方法返回的是索引
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const newFindIdx = arr.find(item => item % 2 === 0)
=> newFindIdx = 1
concat()
该方法在一个数组后面
拼接新的元素,可接收 n 个参数,参数可以是任意数据类型,如果是数组,则将数组跟原数组拼接,如果是其他数据类型,则将该元素添加到原数组后面。 该方法不改变原数组,会返回拼接好的新数组,因此可以执行链式操作
const arr = [1, 3]
const concatArr = arr.concat(2).concat(6,7).concat('hello world').concat([9,[11,12]])
console.warn(arr) => [1, 3]
console.warn(concatArr) => [1, 3, 2, 6, 7, 'hello world', 9, [11,12]]
reverse()
方法将一个数组
倒置,该方法返回新的数组,且会改变原数组
const arr = [1, 2, 3, 4]
const newArr = arr.reverse()
=> arr = [4, 3, 2, 1]
=> newArr = [4, 3, 2, 1]
sort()
方法用于将
数组排序,可以接收一个函数作为参数,当不传递参数时,sort 将按照内部定义的生序的规则进行排序,该方法返回排序后的数组,会改变原数组。
默认的排序算法是调用每个数组项的 toString() 转型方法,比较得到的字符串的编码大小,按照最小值在前面,最大值在后面的方式排序
const arr = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
arr.sort()
=> [1, 10, 2, 3, 4, 5, 6, 7, 8, 9]
接受一个函数作为参数
const arr = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
// 正序
arr.sort((a, b) => a - b)
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
// 降序
arr.sort((a, b) => b - a)
=> [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
// 数组对象元素排序
const arr = [ { id: 4, name: 'qqqq' }, { id: 2, name: 'wwww' }, { id: 3, name: 'eeee' }, { id: 1, name: 'rrrr' },]
arr.sort((a, b) => a.id - b.id) =>
[ { id: 1, name: 'rrrr' }, { id: 2, name: 'wwww' }, { id: 3, name: 'eeee' }, { id: 4, name: 'qqqq' }]
join() 和 split()
join() 方法将数组的元素组起一个字符串,以分隔符分离,省略的话则用默认用
逗号为分隔符,该方法只接收一个参数:即分隔符。该方法返回拼接后的字符串,不改变原数组。
const arr = [1, 3, 'hello']
const newArr = arr.join('-') => '1-3-hello'
split() 方法将一个字符串分割为数组,接收一个参数,以该参数为分割符
const str = '1-3-hello'
consr newStr = str.split('-') => [1, 3, 'hello']
indexOf() 和 lastIndexOf()
两个方法都用来查找索引,接收
两个参数,第一个参数是要查找的元素,第二个可选参数查找的起始位置。都会返回该元素的正索引,不同的是当有第二个参数时,
indexOf() 只查找该元素之后的元素,如无参数二,默认起始位置为第一项
lastIndexOf() 只查找该元素之前的元素,如无参数二,默认起始位置为最后一项
若找到该元素则返回索引,若找不到则返回 -1
const arr = [1, 3, 5, 7, 9]
arr.indexOf(7) => 3
arr.indexOf(3, 2) => -1
slice()
slice(start, end) 接收两个参数,start < end,返回(start, end)区间之间的元素数组,返回的结果包含start位,但不包含end位,
不改变原数组
而且 slice() 可以接收负数作为参数,-1为倒数第一个元素,-2为倒数第二个元素,依此类推
const arr = [1, 2, 3, 4, 5, 6, 7, 8]
arr.slice(0, 2) => [1, 2]
arr.slice(0, 3) => [1, 2, 3]
arr.slice(2, 4) => [3, 4]
arr.slice(0, -7) => [1]
splice()
splice() 从数组中添加/删除项目,然后
返回被删除的元素,该方法会改变原始数组
- 使用方式:splice(index, howmany, item1, item2...itemx)
- 参数index:开始位索引
- 参数howmany:删除的长度
- 参数itemN:删除后插入的元素
const arr = [1, 2, 3, 4, 5]
const spliceArr = arr.splice(0, 1)
arr => [1, 4, 5]
spliceArr => [2, 3]
var arr = [1, 2, 3, 4, 5]
var spliceArr = arr.splice(1, 2, 'qq', 'ww')
arr => [1, 'qq', 'ww', 4, 5]
spliceArr => [2, 3]
reduce()
reduce 为数组中的每一个元素依次执行回调函数,不包括数组中被删除或从未被赋值的元素,接受四个参数:
初始值(或者上一次回调函数的返回值),当前元素值,当前索引,调用 reduce 的数组
使用方式:arr.reduce(callback, initialValue)
- 数组求和:
const arr = [1,2,3,4,5]
const sum = arr.reduce((prev, current) => prev + current, 0)
=> sum = 15
- 数组去重:
const arr = [1,3,4,2,5,3,4]
const newArr = arr.reduce((prev, current) => {
if(prev.includes(current)) {
return prev
} else {
return prev.concat(current)
}
}, [])
=> newArr = [1, 3, 4, 2, 5]
- 对象操作求和
const arr = [
{ id: 4, age: 18, name: 'qqqq' },
{ id: 2, age: 19, name: 'wwww' },
{ id: 3, age: 20, name: 'eeee' },
{ id: 1, age: 21, name: 'rrrr' },
]
const sum = arr.reduce((prev, current) => {
return prev += current.age
}, 0)
console.warn(sum) => 78