js 数组的常用方法

数组的方法有哪些呢?看下图:
在这里插入图片描述
这是定义在数组原型对象上的方法,常用的有:

concat
every
filter
find
findIndex
forEach
includes
indexOf
join
map
pop
push
reduce
shift
slice
splice
some
unshift
// 还有一些也很常用但没有定义在原型对象上的:
isArray
of
from

在日常开发种,经常会用到的基本就这些,剩下的上场率都不高。
接下来,温故一下:

  • concat 拼接数组
    在这里插入图片描述
  • every 对数组中每一项运行给定函数,如果该函数的每一项都返回true,则返回true。
const arr = [1, 2, 3, 4, 5]
const flag = arr.every(item => {
    console.log(item)
    return item < 3
})
console.log(flag)

在这里插入图片描述
当有一项不满足时,直接返回,不继续执行。

  • some 对数组中每一项运行给定函数,如果该函数的某一项都返回true,则返回true。
const arr = [1, 2, 3, 4, 5]
const flag = arr.some(item => {
    console.log(item)
    return item < 3
})

console.log(flag)

在这里插入图片描述
同样的,当有条件满足之后就返回结果,不再继续。

  • filter 返回数组中满足条件的元素组成新的数组,不会改变原数组,也不会对空数组进行检测。
const arr = [1, 2, 3, 4, 5]
const newArr = arr.filter(item => item > 2)

console.log(newArr)

在这里插入图片描述

  • find 根据某个条件查找匹配的值,返回查找到的值。一旦查找到匹配的值将立即停止。
const arr = [1, 2, 3, 4, 5]
const a = arr.find(item => {
    console.log(item)
    return item > 3
})

console.log('a:', a)

在这里插入图片描述

  • findIndex 根据某个条件查找匹配的值,返回查找到的值的下标。一旦查找到匹配的值将立即停止。
const arr = [1, 2, 3, 4, 5]
const a = arr.findIndex(item => {
    console.log(item)
    return item > 3
})

console.log('a:', a)

在这里插入图片描述

  • indexOf 查找某个元素是否存在,存在返回下标,不存在返回-1
const arr = [1, 2, 3, 4, 5]
const a = arr.indexOf(0)
const b = arr.indexOf(1)
console.log('a:', a, 'b:', b)

在这里插入图片描述

  • includes 查找数组中是否存在某个值。在或判断中比较喜欢用这个替代,比如type === 1 || type === 2,可以用[1, 2].includes(type)代替。
const arr = [1, 2, 3, 4, 5]
const a = arr.includes(0)
const b = arr.includes(1)
console.log('a:', a, 'b:', b)

在这里插入图片描述

  • join 数组转字符串
const arr = [1, 2, 3, 4, 5]
const a = arr.join()
const b = arr.join('-')
console.log('a:', a, 'b:', b)

在这里插入图片描述

  • forEach 遍历 没有返回值,即使显示的返回。
const arr = [1, 2, 3, 4, 5]
const a = arr.forEach(item => {
    console.log(item)
    return '返回的值' + item
})
console.log('a:', a)

在这里插入图片描述

  • map 映射 有返回值,即使没有显示的return。
const arr = [1, 2, 3, 4, 5]
const a = arr.map(item => {
    console.log(item)
    return '返回的值' + item
})
console.log('a:', a)

在这里插入图片描述

  • reduce 接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终为一个值,是es5新增的一个数组逐项处理的方法。具体可以查看reduce学习笔记
  • slice 从已有的数组中返回选定的元素。接收两个参数start,end。返回一个新的数组,包含从 start 到 end (不包括该元素)的数组中的元素。
const arr = [1, 2, 3, 4, 5]
const a = arr.slice(1, 2)
console.log('a:', a)

在这里插入图片描述

  • splice 从数组中添加/删除元素,然后返回被删除的元素。
const arr = [1, 2, 3, 4, 5]
const a = arr.splice(1, 0, 6)
const b = arr.splice(0, 1)
console.log('a:', a, 'b:', b, arr)

在这里插入图片描述

  • isArray 判断是否是数组
const arr = [1, 2, 3, 4, 5]
console.log(Array.isArray(arr))

在这里插入图片描述

  • of es6新增的创建数组的方法
const arr1 = Array.of(1,2,3)
console.log(arr1)

在这里插入图片描述

  • from 将非数组对象转换成真实数组
const arr1 = new Set([1,2,3])
const arr2 = Array.from(arr1)
console.log(arr1, arr2)

在这里插入图片描述

  • push 往数组里面添加元素,添加到数组末尾
  • pop 从数组里面删除元素,从末尾开始删除
  • unshift 往数组里面添加元素,添加到数组开头
  • shift 从数组里面删除元素,从开头开始删除
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JS数组常用方法有很多,以下是其中的一些常用方法: 1. Array.push():向数组的末尾添加一个或多个元素,并返回新的数组长度。原数组改变。 2. Array.pop():删除数组的最后一个元素,并返回删除的元素。原数组改变。 3. Array.shift():删除数组的第一个元素,并返回删除的元素。原数组改变。 4. Array.unshift():向数组的开头添加一个或多个元素,并返回新的数组长度。原数组改变。 5. Array.reverse():反转数组的顺序。原数组改变。 6. Array.sort():对数组进行排序。原数组改变。 7. Array.splice():从数组中删除元素,并可以在指定位置插入新的元素。原数组改变。 8. Array.concat():合并两个或多个数组,生成一个新的数组。原数组不变。 9. Array.join():将数组的所有元素连接成一个字符串。原数组不变。 10. Array.indexOf():返回指定元素在数组中的索引,如果不存在则返回-1。 11. Array.slice():从指定位置截取数组的片段并返回新的数组。原数组不变。 12. Array.forEach():对数组的每个元素执行指定的操作。 13. Array.map():对数组的每个元素执行指定的操作,并返回一个新的数组。 14. Array.filter():根据指定的条件过滤数组的元素,并返回一个新的数组。 15. Array.every():检测数组的所有元素是否都满足指定的条件。 16. Array.some():检测数组的是否存在满足指定条件的元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值