JS成神之路(二):js开发数组常用方法

1.创建数组的三种方式

let arr =[value1,value2,value3]  //最常用的方式
let arr =new Array(value1,value2,value3) //最不常用的方式
let arr =new Array(length) //定义数组的长度方式

2.遍历数组的四种方式

arr.forEach(function(item,index,array){   })//数组的每一个元素都可以用函数处理,较为常用
for(let i=0;i<arr.length;i++){   } //最老使用的一种数组遍历方式,速度最快
for(let item of arr){ }//无法获得key值
for(let key in arr){  }//可以获得不想要的key,不要使用,可能获得我们不需要的key值

3.查询数组元素的方法

arr.find/findIndex(function(accumulator, item, index, array) {})//返回符合条件的值或者值索引
arr.filter(function(accumulator, item, index, array) {})//返回符合条件的集合
includes(value) — 如果数组有 value,则返回 true,否则返回 false。
indexOf/lastIndexOf(item, pos) — 从位置 pos 开始搜索 item,搜索到则返回该项的索引,否则返回 -1

4.转换数组

arr.map(function(accumulator, item, index, array) {})//对值进行操作后返回新数组
arr.reduce(function(accumulator, item, index, array) {})//对数组的值进行累加后,返回一个值
sort(func) — 对数组进行原位(in-place)排序,然后返回它。
reverse() — 原位(in-place)反转数组,然后返回它。
split/join — 将字符串转换为数组并返回。
Array.from(values,function(item,){},) ——将字符串,数组,集合,类数组对象,迭代对象转换为数组

5.操作数组

arr.pop();arr.push//数组的末尾弹出,和末尾添加,效率最高
arr.shift();arr.unshift()//数组的首端弹出,和首端添加,效率最低
arr.splice(startIndex,deleteCount,subValue)//可以对数组进行增删查改
arr.slice(begin,end)//截取一段数组
arr.concat(value1,value2)//组合成为一个新的数组
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值