js数组的遍历

Symbol卢
• 阅读 1388

js中数组的遍历

1. Map()方法

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

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

2.forEach()方法

语法:

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

案例:遍历数组对象

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

案例:遍历普通的数组

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

forEach()和map()的区别:

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

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


3.for... in 方法

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

案例:

``//遍历数组
const arr = [1111, 2222, 3333];
for (let i in arr) {
 console.log(`索引:${i}  -------  值:${arr[i]}`);
}
console.log("--------漂亮的分割线-------");
//遍历对象
let obj = {
 userName: 'symbol卢',
 age: 18,
 sex:"男"
};
for (let i in obj) {
 console.log(`key:${i}   -----  value: ${obj[i]}`)
}`` 

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

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

4.for…of 方法

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

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

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

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

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


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

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

6.filter 过滤器函数

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

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

7.some() 方法

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

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

8. every() 方法

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

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

js数组的遍历

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

9. find() 方法

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

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

js数组的遍历

10.findIndex()方法

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

`const arr = [
 { id: 1, name: '电脑', active: false },
 { id: 2, name: '手机', active: true },
 { id: 3, name: '数码相机', active: true },
 { id: 4, name: 'U盘', active: true },
 { id: 5, name: '机械键盘', active: false }
];  
const res = arr.findIndex((item,index,arr)=>{
 return item.active;
});
console.log(res);//1
//参数1:数组中的每一项(必选)
//参数2:索引(可选)
//参数3:当前遍历的数组本身(可选)` 
点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
Dax Dax
3年前
js高频手写大全
1.手写instanceofinstanceof作用:判断一个实例是否是其父类或者祖先类型的实例。instanceof在查找的过程中会遍历左边变量的原型链,直到找到右边变量的prototype查找失败,返回false2.实现数组的map方法3.reduce实现数组的map方法4.手写数组的reduce方法reduce()方法接收一个函数作为累
翼
3年前
js 数组 转为树形结构
需要转换为树形的数组vardata{"orderById":null,"platformCommissionProportion":1,"name":"添加剂","pid":13,"id":26
Karen110 Karen110
2年前
盘点JavaScript中数组遍历的全部方式(上篇)
前言JavaScript想必大家都不陌生了,其中的字符串和数组大家经常都会用到,今天就让我们来说说这里面的数组对象的遍历吧,因为遍历经常使用的缘故,所以小编带着大家来解锁遍历的所有方法,以便大家能够更深入的了解数组遍历,并在实际项目中灵活运用。一、Entries这个是ES6中提供的用于遍历数组的方法,它会返回一个遍历器对象,Entries是对键值对的遍历。
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
JavaScript中遍历数组和对象的方法
js数组遍历和对象遍历针对js各种遍历作一个总结分析,从类型用处,分析数组和对象各种遍历使用场景,优缺点等JS数组遍历:1,普通for循环,经常用的数组遍历vararr1,2,0,3,9,10,20,
Stella981 Stella981
2年前
JS 对象数组Array 根据对象object key的值排序sort,很风骚哦
有个js对象数组varary\{id:1,name:"b"},{id:2,name:"b"}\需求是根据name或者id的值来排序,这里有个风骚的函数函数定义:function keysrt(key,desc) {  return function(a,b){    return desc ? ~~(ak
Stella981 Stella981
2年前
JavaScript遍历循环
定义一个数组和对象constarr'a','b','c','d','e','f';constobj{a:1,b:2,c:3,d:4}for()经常用来遍历数组元素遍历值为数组元素索引f
Stella981 Stella981
2年前
ES6 for in与for of 的使用方法及其区别
  // for in遍历的是数组的索引(即键名),而for of遍历的是数组元素值。 let arr  \1,2,3,4,5,6,7\    for(let index of arr){     //   console.log(index)//1 2 3 4 5 6 7    }    for(let index in
Wesley13 Wesley13
2年前
5种方式实现数组扁平化
数组扁平化概念数组扁平化是指将一个多维数组变为一维数组1,2,3,4,51,2,3,4,5实现1\.reduce遍历数组每一项,若值为数组则递归遍历,否则concat。functionflatten(arr){