当前位置:网站首页>ES6 array traversal and Es5 array traversal
ES6 array traversal and Es5 array traversal
2022-06-30 06:05:00 【SignalFire】
One 、ES6 Array traversal
(1)find Returns the first qualified element in the array
let result = arr.find( (val) => {
return val % 2 == 0;
});
console.log(result);
(2) findIndex, Returns the index of the first eligible element
let result = arr.findIndex( (val) => {
return val % 2 == 0;
});
console.log(result);
(3) for of
Get value
for(let item of arr){
console.log(item);
}
for(let item of arr.values()){
console.log(item);
}
Get index
for(let index of arr.keys()){
console.log(index);
}
Get values and indexes
for(let [index,item] of arr.entries()){
console.log(index,item);
}
Two 、ES5 Array traversal
(1)forEach, I won't support it continue and break
arr.forEach((item,index,array) => {
console.log(item,index);
});
(2)map Traverse , Pass in a callback function , Returns a new array
let result = arr.map((val) => {
val ++;
return val;
})
console.log(arr,result);
(3)filter Traverse , Incoming callback function , Return the filtered array
let result = arr.filter( (val) => {
return val % 2 == 0;
});
console.log(result);
(4)some, Returns whether there is a value to detect , return Boolean
let result = arr.some( (val) => {
return val == 2;
});
console.log(arr,result);
(5)every, Only when each element matches, it returns true
let result = arr.every( (val) => {
return val % 2 == 0;
});
console.log(result);
(6)reduce
duplicate removal
let result = arr.reduce( (pre,cur) => {
pre.indexOf(cur) == -1 && pre.push(cur);
return pre;
},[]);
console.log(result);
Add up
let result = arr.reduce( (pre,cur) => {
return pre + cur;
});
console.log(result);
Maximum
let result = arr.reduce( (pre,cur) => {
return Math.max(pre,cur);
});
console.log(result);
(7)for in
for in Will be able to Array.prorotype The custom methods on the are also iterated out , It is not recommended to use for in Traversal array
Array.prototype.myFn = function(){};
for(index in arr){
console.log(index,arr[index]);
}
边栏推荐
- Official win 10 image download
- Swoole process model diagram
- 反編譯正常回編譯出現問題自己解决辦法
- [exercise] basic practice letter graph of Blue Bridge Cup
- Use of tornado template
- InputStream to inputstreamsource
- 二十四、输入输出设备模型(串口/键盘/磁盘/打印机/总线/中断控制器/DMA和GPU)
- Problems related to pinduoduo store search, why can't new products be found on the shelves
- Huxiaochun came to fengshu electronics to sign a strategic cooperation agreement with Zoomlion
- 24、 I / O device model (serial port / keyboard / disk / printer / bus / interrupt controller /dma and GPU)
猜你喜欢

MySQL transaction

MySQL log management, data backup and recovery
![[OSPF] comparison between rip and OSPF](/img/72/00e3a05bc5de0e5a66b4675d030911.jpg)
[OSPF] comparison between rip and OSPF

Mysql database learning notes - foreign keys, table connections, subqueries, and indexes for MySQL multi table queries

接口中方法详解
![[MD editing required] welcome to the CSDN markdown editor](/img/6c/df2ebb3e39d1e47b8dd74cfdddbb06.gif)
[MD editing required] welcome to the CSDN markdown editor

ECS deployment web project
一个完整的性能测试流程

1380. lucky numbers in matrices

About modifying dual system default startup item settings
随机推荐
Share problems solved
MySQL高级SQL语句
接口中方法详解
Codeforces B. MEX and Array
Mysql database learning notes - foreign keys, table connections, subqueries, and indexes for MySQL multi table queries
VIM view file code
[deep learning] data segmentation
Sword finger offer 29 Print matrix clockwise
Today, Ali came out with 35K. It's really sandpaper that wiped my ass. it showed me my hand
How to print pthread_ t - How to print pthread_ t
STM32F103系列控制的OLED IIC 4针
inno setup 最简单的自定义界面效果
谁不想要一个自己的博客网站呢 - 搭建博客网站wordpress
What indicators should safety service engineers pay attention to in emergency response?
Ten years' miscellaneous thoughts
Shenzhou ares tx6 boot logo modification tutorial
Cisco VXLAN配置
STM32F103 series controlled OLED IIC 4-pin
10-【istio】istio Sidecar
Lantern Festival | maoqiu technology and everyone "guess riddles and have a good night"