当前位置:网站首页>ES6数组遍历与ES5数组遍历
ES6数组遍历与ES5数组遍历
2022-06-30 05:58:00 【SignalFire】
一、ES6数组遍历
(1)find 返回数组中第一个符合条件的元素
let result = arr.find( (val) => {
return val % 2 == 0;
});
console.log(result);
(2) findIndex,返回第一个符合条件的元素的索引
let result = arr.findIndex( (val) => {
return val % 2 == 0;
});
console.log(result);
(3) for of
获取值
for(let item of arr){
console.log(item);
}
for(let item of arr.values()){
console.log(item);
}
获取索引
for(let index of arr.keys()){
console.log(index);
}
获取值和索引
for(let [index,item] of arr.entries()){
console.log(index,item);
}
二、ES5数组遍历
(1)forEach,不支持continue和break
arr.forEach((item,index,array) => {
console.log(item,index);
});
(2)map遍历,传入一个回调函数,返回一个新数组
let result = arr.map((val) => {
val ++;
return val;
})
console.log(arr,result);
(3)filter遍历,传入回调函数,返回过滤后的数组
let result = arr.filter( (val) => {
return val % 2 == 0;
});
console.log(result);
(4)some,返回是否存在要检测的值,返回Boolean
let result = arr.some( (val) => {
return val == 2;
});
console.log(arr,result);
(5)every,只有每个元素都符合才返回true
let result = arr.every( (val) => {
return val % 2 == 0;
});
console.log(result);
(6)reduce
去重
let result = arr.reduce( (pre,cur) => {
pre.indexOf(cur) == -1 && pre.push(cur);
return pre;
},[]);
console.log(result);
累加
let result = arr.reduce( (pre,cur) => {
return pre + cur;
});
console.log(result);
最大值
let result = arr.reduce( (pre,cur) => {
return Math.max(pre,cur);
});
console.log(result);
(7)for in
for in 会把Array.prorotype上的自定义方法也遍历出来,不推荐使用for in 遍历数组
Array.prototype.myFn = function(){};
for(index in arr){
console.log(index,arr[index]);
}
边栏推荐
- [Blue Bridge Road -- bug free code] DS1302 time module code analysis
- 强烈推荐十几款IDEA开发必备的插件
- 86. separate linked list
- Xi'an Jiaotong 21st autumn "computerized accounting" online homework answer sheet (I) [standard answer]
- Leader: who can use redis expired monitoring to close orders and get out of here!
- Related applications of priority queue
- luoguP2756 飞行员配对方案问题(最大流)
- Huxiaochun came to fengshu electronics to sign a strategic cooperation agreement with Zoomlion
- Detailed explanation of issues related to SSL certificate renewal
- 声网,站在物联网的“土壤”里
猜你喜欢

Did you know that WPS can turn on eye protection mode?

Transfer the token on the matic-erc20 network to the matic polygon

Inno setup the simplest user-defined interface effect

MySQL advanced SQL statement

Sword finger offer 18 Delete the node of the linked list

How to automatically renew a token after it expires?
![[ansible series] fundamentals 02 module debug](/img/99/c53be8e2a42c7cb5b4a9a7ef4ad98c.jpg)
[ansible series] fundamentals 02 module debug

Sound network, standing in the "soil" of the Internet of things

We strongly recommend more than a dozen necessary plug-ins for idea development

Configure the user to log in to the device through telnet -- AAA local authentication
随机推荐
Sound net, debout dans le "sol" de l'IOT
MySQL log management, data backup and recovery
[untitled] user defined function
Turn off automatic outlining in Visual Studio - turning off automatic outlining in Visual Studio
Inno setup the simplest user-defined interface effect
Implementation of property management system with ssm+ wechat applet
C language code record
Gestion des utilisateurs de la base de données MySQL
Official win 10 image download
[Alibaba cloud] student growth plan answers
Today, Ali came out with 35K. It's really sandpaper that wiped my ass. it showed me my hand
【板栗糖GIS】global mapper—如何把栅格的高程值赋予给点
[road of system analyst] collection of wrong topics in Project Management Chapter
Idea of capturing mobile terminal variant combination
谁不想要一个自己的博客网站呢 - 搭建博客网站wordpress
Learning automation ppt
动态规划--怪盗基德的滑翔翼
09- [istio] istio service entry
Dynamic programming -- gliding wing of the strange thief Kidd
STM32F103 series controlled OLED IIC 4-pin