当前位置:网站首页>Several commonly used traversal methods
Several commonly used traversal methods
2022-07-25 23:12:00 【Dignified 304】
Sort out several commonly used traversal methods ,for and while The cycle is no longer said , Let's talk about several other very useful methods
1.for in
for in It must be the method of traversing arrays that contacts earlier , In fact, it can also traverse objects ( Traversal of strings is also possible !) Example demonstration
Traversal array
<script>
var arr = ['a', 'b', 'c', 'd'];
// Traversal array key It's the index
for (var key in arr) {
console.log(arr[key] + '---' + key);
}
</script>
Traversing objects
var obj = {
name: ' Zhang San ',
age: '20',
sex: ' male ',
}
// Traversing objects key Attribute obj[key] It's property value
for (let key in obj) {
console.log(obj[key] + '---' + key);
}
Traversal string Similar to arrays
// Traversal string key It's the index
var str = 'abcd';
for (let key in str) {
console.log(str[key] + '---' + key);
}2.for of (ES6)
p It's every element in the array ,for of Traversing iteratable data , Can't traverse object !!!
var arr = ['a', 'b', 'c', 'd'];
// Traversal array key It's the index
for (let p of arr) {
console.log(p);// Output a b c d
}
3.forEach() Method performs the given function once for each element of the array .
var arr = ['a', 'b', 'c', 'd'];
// Traversal array element It's every element
arr.forEach((element) => {
console.log(element);
});
High-order usage
grammar
arr.forEach(callback(currentValue [, index [, array]])[, thisArg])
Parameters
callback
Functions executed for each element in the array , This function takes one to three parameters :
currentValue
The current element being processed in the array .
index Optional
The index of the current element being processed in the array .
array Optional
forEach() Method is operating on the array .
thisArg Optional
Optional parameters . When the callback function is executed callback when , Used as a this Value .
4.find() Returns the first element that satisfies the condition , Otherwise return to undefined
<script>
var arr = [1, 2, 3, 4, 5];
// Traversal array element It's every element
const a = arr.find(element => element > 3);
console.log(a);// Output 4
</script>
5.findIndex Return the first index that meets the condition , Otherwise return to -1
<script>
var arr = [1, 2, 3, 4, 5];
// Traversal array element It's every element
const a = arr.findIndex(element => element > 3);
console.log(a);// Output 3
</script>6.filter() All the elements that meet the conditions form a new array and return , Otherwise it's an empty array
<script>
var arr = [1, 2, 3, 4, 5];
// Traversal array element It's every element
const a = arr.filter(element => element > 3);
console.log(a);
</script>
7.Object.keys(obj) You can traverse objects , You can also iterate over groups
<script>
var arr = [1, 'a', 3, 4, 5];
var obj = { a: 1, b: 2 }
// Traverse the array and return the index of each element
let a = Object.keys(arr);
console.log(a);
// Traverse the object, convert each attribute in the object into characters, form a new array, and return
let b = Object.keys(obj);
console.log(b);
</script>
边栏推荐
- WordPress removes the website publishing time
- Check code generation
- 【论文笔记】A Meta-Reinforcement Learning Algorithm for Causal Discovery
- Tencent map API request source is not authorized, this request source domain name
- 每周推荐短视频:需要协同的智能设备越来越多,给物联网开发提出更大挑战?
- Shanghai Second Polytechnic University - Health Daily autocheck
- MathType安装和解决不能Crtl+V的问题
- 内存分页与调优,内核与用户空间
- Unity uses macros
- MathType installation and solution cannot solve the problem of crtl+v
猜你喜欢

Mongodb的特点、与MySQL的差别、以及应用场景

The small icon of notification setting shows a small square

Unity 使用宏

Network Security Learning (16)

Secure code warrior learning record (IV)

Tencent map API request source is not authorized, this request source domain name

Mongodb features, differences with MySQL, and application scenarios

Network Security Learning notes-1 file upload

单模型常识推理首超人类!HFL登顶OpenBookQA挑战赛

The new UI people help task help PHP source code with a value of 1500 / reward task Tiktok Kwai headline like source code / with three-level distribution can be packaged applet
随机推荐
第二周学习:卷积神经网络
Firewall command simple operation
HJ9 提取不重复的整数
PCL basic operation Encyclopedia
推荐系统——An Embedding Learning Framework for Numerical Features in CTR Prediction
Stack and stack class
IPFs of Internet Protocol
网格参数化Least Squares Conformal Maps实现(3D网格映射到2D平面)
Memory paging and tuning, kernel and user space
How to obtain the cash flow data of advertising services to help analyze the advertising effect?
Hj7 take approximate value
Ssh server CBC encryption mode vulnerability (cve-2008-5161)
Recommend short videos every week: more and more smart devices need collaboration, posing a greater challenge to the development of the Internet of things?
MES系统设备管理概述(下)
内存分页与调优,内核与用户空间
7-1 understand everything
Simulink learning notes (III) - Simulink automatic code generation (II) "suggestions collection"
Secure code warrior learning record (II)
类和对象(2)(6个默认成员函数)
PHP wechat scan code, follow official account and authorize login source code
