js数组的遍历

js中数组的遍历

1. Map()方法

Map 映射的意思,可以看做是一个映射函数,所谓的映射。一个萝卜一个坑,一 一对应的关系;

语法:
1`const arr=Array(); 2arr.map((item,index,arr)=>{ 3 //函数体 4 return ; 5}); 6//参数1:数组中的每一项(必选) 7//参数2:索引(可选) 8//参数3:当前遍历的数组本身(可选) 9//注意,map()不能对空的数组进行操作`
案例
1`const arr=[1,2,3,4]; 2const arr2=arr.map((item)=>{ 3 item+1; 4}); 5console.log(arr2);`

2.forEach()方法

语法:

1`const arr = [11,22,33,44,55,66,77,88]; 2arr.forEach((item,index,arr)=>{ 3 }); 4//参数1:数组中的每一项(必选) 5//参数2:索引(可选) 6//参数3:当前遍历的数组本身(可选)`

案例:遍历数组对象

1`const arr = [ 2 {id:1,title:'标题1标题1标题1'}, 3 {id:2,title:'标题2标题2标题2'}, 4 {id:3,title:'标题3标题3标题3'} 5 ]; 6arr.forEach((item)=>{ 7 console.log(item.id+ '---' +item.title) 8})`

案例:遍历普通的数组

1``const arr = [11,22,33,44,55,66,77,88]; 2arr.forEach((item,index)=>{ 3 console.log(`${item}-----${index+1}`); 4})``

forEach()和map()的区别:

1、forEach():用来遍历数组中的每一项,这个方法执行没有返回值,不影响原数组

2、map():相当与原数组克隆了一份,对克隆的数据进行操作,需要return, 返回一个新的数组,不影响原数组


3.for... in 方法

for...in既可以遍历数组,也可以遍历对象

案例:

1``//遍历数组 2const arr = [1111, 2222, 3333]; 3for (let i in arr) { 4 console.log(`索引:${i} ------- 值:${arr[i]}`); 5} 6console.log("--------漂亮的分割线-------"); 7//遍历对象 8let obj = { 9 userName: 'symbol卢', 10 age: 18, 11 sex:"男" 12}; 13for (let i in obj) { 14 console.log(`key:${i} ----- value: ${obj[i]}`) 15}``

for...in 在**遍历数组的时候,变量 i 指的是数组的 索引**

for...in在遍历对象的时候,变量 i 指的是 对象的 key

4.for…of 方法

for...of遍历数组,遍历的是数组的 value (每一项)

1`const arr = [1111, 2222, 3333]; 2for (let i of arr) { 3 console.log(i); 4}`

for...in 和 for..of的区别:

1,for..in既可以遍历数组,也可以遍历对象,在遍历数组的时候 i表示的是 数组的索引,遍历对象的时候,遍历的是对象的key

2,for...of只能遍历数组,不能遍历对象,遍历数组的时候,i 表示的是数组的 值


5.for 循环(最常见的循环)

1`const arr = [1111, 2222, 3333]; 2for(let i=0;i<arr.length;i++){ 3 console.log(arr[i]); 4} 5//优化 使用临时变量,将长度缓存起来,避免重复获取数组长度,当数组较大时优化效果才会比较明显 6var arr = [1111, 2222, 3333]; 7var len = arr.length; 8for(var i = 0; i < len; i++) { 9 console.log(arr[i]); 10}`

6.filter 过滤器函数

遍历数组,过滤出符合条件的元素并返回一个新数组

1`const arr = [ 2 { id: 1, name: '电脑', active: true }, 3 { id: 2, name: '手机', active: true }, 4 { id: 3, name: '数码相机', active: false }, 5 { id: 4, name: 'U盘', active: true }, 6 { id: 5, name: '机械键盘', active: false } 7]; 8const newArr = arr.filter((item, index,arr)=>{ 9 return item.active; 10}); 11console.log(newArr); 12//参数1:数组中的每一项(必选) 13//参数2:索引(可选) 14//参数3:当前遍历的数组本身(可选)`

7.some() 方法

****** 遍历数组,数组中只要有一个元素满足条件就返回 true 并且终止遍历,否则返回 false;

1`const arr = [ 2 { id: 1, name: '电脑', active: false }, 3 { id: 2, name: '手机', active: true }, 4 { id: 3, name: '数码相机', active: false }, 5 { id: 4, name: 'U盘', active: true }, 6 { id: 5, name: '机械键盘', active: false } 7]; 8const res = arr.some((item, index,arr)=>{ 9 return item.active; 10}); 11console.log(res); 12//参数1:数组中的每一项(必选) 13//参数2:索引(可选) 14//参数3:当前遍历的数组本身(可选)`

8. every() 方法

****** 遍历数组,数组中的每一个元素都满足条件 则返回 true,否则返回 false

1`const arr = [ 2 { id: 1, name: '电脑', active: true }, 3 { id: 2, name: '手机', active: true }, 4 { id: 3, name: '数码相机', active: false }, 5 { id: 4, name: 'U盘', active: true }, 6 { id: 5, name: '机械键盘', active: false } 7]; 8const res = arr.every((item, index,arr)=>{ 9 console.log(item); 10 console.log(index); 11 console.log(arr); 12 return item.active; 13}); 14console.log(res); 15//参数1:数组中的每一项(必选) 16//参数2:索引(可选) 17//参数3:当前遍历的数组本身(可选)`

在遍历的过程中,元素满足条件,则会继续向下遍历,如果不满足条件,则会终止循环

9. find() 方法

****** 遍历数组,返回满足条件的第一个元素 后终止循环,如果没有符合条件的元素则返回 undefined

1`const arr = [ 2 { id: 1, name: '电脑', active: false }, 3 { id: 2, name: '手机', active: true }, 4 { id: 3, name: '数码相机', active: true }, 5 { id: 4, name: 'U盘', active: true }, 6 { id: 5, name: '机械键盘', active: false } 7]; 8const res = arr.find((item,index,arr)=>{ 9 return item.active; 10}); 11console.log(res); 12//参数1:数组中的每一项(必选) 13//参数2:索引(可选) 14//参数3:当前遍历的数组本身(可选)`

10.findIndex()方法

遍历数组,返回满足条件的第一个元素的 **索引 ** 后终止循环,如果没有符合条件的元素则返回 -1

1`const arr = [ 2 { id: 1, name: '电脑', active: false }, 3 { id: 2, name: '手机', active: true }, 4 { id: 3, name: '数码相机', active: true }, 5 { id: 4, name: 'U盘', active: true }, 6 { id: 5, name: '机械键盘', active: false } 7]; 8const res = arr.findIndex((item,index,arr)=>{ 9 return item.active; 10}); 11console.log(res);//1 12//参数1:数组中的每一项(必选) 13//参数2:索引(可选) 14//参数3:当前遍历的数组本身(可选)`
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

js 数组 转为树形结构

需要转换为树形的数组vardata{"orderById":null,"platformCommissionProportion":1,"name":"添加剂","pid":13,"id":26

盘点JavaScript中数组遍历的全部方式(上篇)

前言JavaScript想必大家都不陌生了,其中的字符串和数组大家经常都会用到,今天就让我们来说说这里面的数组对象的遍历吧,因为遍历经常使用的缘故,所以小编带着大家来解锁遍历的所有方法,以便大家能够更深入的了解数组遍历,并在实际项目中灵活运用。一、Entries这个是ES6中提供的用于遍历数组的方法,它会返回一个遍历器对象,Entries是对键值对的遍历。