当前位置:网站首页>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]);
}
边栏推荐
- MySQL高级SQL语句
- MySQL transaction
- 24、 I / O device model (serial port / keyboard / disk / printer / bus / interrupt controller /dma and GPU)
- Title: enter two positive integers m and N to find their maximum common divisor and minimum common multiple
- 【数据库】事务
- Finally someone can make the server so straightforward
- Attempt to redefine 'timeout' at line 2 solution
- MySQL数据库用户管理
- luoguP2756 飞行员配对方案问题(最大流)
- 反編譯正常回編譯出現問題自己解决辦法
猜你喜欢

Golang之手写web框架
![[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

The average salary of software testing in 2022 has been released. Have you been averaged?

MySQL数据库用户管理

Configure the user to log in to the device through telnet -- AAA local authentication

MySQL高级SQL语句

Swoole process model diagram

MySQL storage system

MySQL advanced (Advanced SQL statement)
![[deep learning] data segmentation](/img/16/798881bbee66faa2fb8d9396155010.jpg)
[deep learning] data segmentation
随机推荐
At the age of 32, I fell into a middle-aged crisis and finally quit naked...
Dynamic programming -- gliding wing of the strange thief Kidd
Intelligent deodorizer embedded development
STM32F103系列控制的OLED IIC 4针
反编译正常回编译出现问题自己解决办法
8 ways to earn passive income
从底层结构开始学习FPGA----RAM IP核及关键参数介绍
VLAN access mode
文件操作IO-Part1
Codeforces B. MEX and Array
Develop stylelint rules from zero (plug-ins)
雲服務器部署 Web 項目
Base64详解:玩转图片Base64编码
Create priority queue
IP TCP UDP network encryption suite format
动态规划--怪盗基德的滑翔翼
Record a problem tracking of excessive load
[road of system analyst] collection of wrong topics in Project Management Chapter
[ansible series] fundamentals 02 module debug
MySQL事物