当前位置:网站首页>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]);
}
边栏推荐
- Leader: who can use redis expired monitoring to close orders and get out of here!
- Codeforces C. Andrew and Stones
- [GPU] basic operation of GPU (I)
- 10-【istio】istio Sidecar
- Transfer the token on the matic-erc20 network to the matic polygon
- Prototype and prototype chain in JS
- UE4_ Editor development: highlight the UI making method according to the assets dragged by the mouse (1)
- Use of tornado template
- MySQL advanced SQL statement
- [OSPF] comparison between rip and OSPF
猜你喜欢

Create priority queue

Codeforces B. MEX and Array

The average salary of software testing in 2022 has been released. Have you been averaged?
![[ansible series] fundamentals -01](/img/b4/1f3284338c75acb5259849a45bbfbe.jpg)
[ansible series] fundamentals -01

PC viewing WiFi password
![[GPU] basic operation of GPU (I)](/img/ce/0ca8c63525038fea64c40aabd17fc6.jpg)
[GPU] basic operation of GPU (I)
![09- [istio] istio service entry](/img/48/86f8ec916201eefc6ca09c45a60a6a.jpg)
09- [istio] istio service entry

leetcode763. Divide letter interval

重构之美:当多线程批处理任务挑起大梁 - 万能脚手架

Projet Web de déploiement du serveur Cloud
随机推荐
1380. lucky numbers in matrices
重构之美:当多线程批处理任务挑起大梁 - 万能脚手架
Video summary of my station B
InputStream to inputstreamsource
Acwing winter vacation daily question 2022 punch in day 11
inno setup 最简单的自定义界面效果
Sound network, standing in the "soil" of the Internet of things
You don't know how to deduce the location where HashSet stores elements?
[Alibaba cloud] student growth plan answers
C language code record
Master slave synchronization of MySQL database to realize read-write separation
Mysql database user management
Set of XXL job principles
MySQL高级SQL语句
At the beginning of 2022, people who are ready to change jobs should pay attention to
What kind of answer has Inspur given in the big AI model landing test?
Rotating box target detection mmrotate v0.3.1 getting started
MySQL advanced (Advanced SQL statement)
Title: enter two positive integers m and N to find their maximum common divisor and minimum common multiple
Summation of basic exercise sequence of test questions