当前位置:网站首页>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]);
}
边栏推荐
- Navigate back to fragmentpageradapter - & gt; Fragment is empty - navigating back to fragmentpageradapter - & gt; fragments are empty
- MySQL advanced SQL statement
- Application of redis client list in practice
- CompletionService使用及原理(源码分析)
- How does WPS cancel automatic numbering? Four options
- Golang之手写web框架
- Mysql database learning notes - foreign keys, table connections, subqueries, and indexes for MySQL multi table queries
- ES6扩展运算符(...)
- ES6箭头函数
- Ultra simple STM32 RTC alarm clock configuration
猜你喜欢

STM32F103系列控制的OLED IIC 4针

Cisco VXLAN配置

谁不想要一个自己的博客网站呢 - 搭建博客网站wordpress
![[ansible series] fundamentals 02 module debug](/img/99/c53be8e2a42c7cb5b4a9a7ef4ad98c.jpg)
[ansible series] fundamentals 02 module debug

Golang's handwritten Web Framework

STM32F103 series controlled OLED IIC 4-pin
![[road of system analyst] collection of wrong topics in Project Management Chapter](/img/8b/2908cd282f5e505efe5223b4c5ddbf.jpg)
[road of system analyst] collection of wrong topics in Project Management Chapter

MySQL 索引

云服务器部署 Web 项目

Mysql database user management
随机推荐
PC viewing WiFi password
Base64详解:玩转图片Base64编码
The average salary of software testing in 2022 has been released. Have you been averaged?
Configure the user to log in to the device through telnet -- AAA local authentication
Huxiaochun came to fengshu electronics to sign a strategic cooperation agreement with Zoomlion
数据读写:Unity中基于C#脚本实现数据读写功能
Es6数组
ES6扩展运算符(...)
How to use unmarshaljson
InputStream to inputstreamsource
How to print pthread_ t - How to print pthread_ t
[GPU] basic operation of GPU (I)
CompletionService使用及原理(源码分析)
Projet Web de déploiement du serveur Cloud
Golang's handwritten Web Framework
Cisco VXLAN配置
46. 全排列-dfs双百代码
24、 I / O device model (serial port / keyboard / disk / printer / bus / interrupt controller /dma and GPU)
VIM view file code
Feisheng: Based on the Chinese word breaker ik-2 ways to build custom hot word separators Showcase & pit arrangement Showtime