当前位置:网站首页>几种常用的遍历方法
几种常用的遍历方法
2022-07-19 05:06:00 【道貌岸然304】
整理一下常用的几种遍历方法,for和while循环就不再说了,说一下其他几种非常好用的方法
1.for in
for in可定是接触比较早的遍历数组的方法,其实它也可遍历对象(遍历字符串也是可以的!)实例演示一下
遍历数组
<script>
var arr = ['a', 'b', 'c', 'd'];
// 遍历数组 key是索引
for (var key in arr) {
console.log(arr[key] + '---' + key);
}
</script>
遍历对象
var obj = {
name: '张三',
age: '20',
sex: '男',
}
// 遍历对象 key是属性 obj[key]是属性值
for (let key in obj) {
console.log(obj[key] + '---' + key);
}
遍历字符串 和数组类似
// 遍历字符串 key是索引
var str = 'abcd';
for (let key in str) {
console.log(str[key] + '---' + key);
}
2.for of (ES6)
p就是数组中的每个元素,for of 遍历可迭代数据,不能遍历对象!!!
var arr = ['a', 'b', 'c', 'd'];
// 遍历数组 key是索引
for (let p of arr) {
console.log(p);//输出a b c d
}
3.forEach()方法对数组的每个元素执行一次给定的函数。
var arr = ['a', 'b', 'c', 'd'];
// 遍历数组 element是每个元素
arr.forEach((element) => {
console.log(element);
});
高阶用法
语法
arr.forEach(callback(currentValue [, index [, array]])[, thisArg])
参数
callback
为数组中每个元素执行的函数,该函数接收一至三个参数:
currentValue
数组中正在处理的当前元素。
index
可选
数组中正在处理的当前元素的索引。
array
可选
forEach()
方法正在操作的数组。
thisArg
可选
可选参数。当执行回调函数 callback
时,用作 this
的值。
4.find() 返回第一个满足条件的元素,否则返回undefined
<script>
var arr = [1, 2, 3, 4, 5];
// 遍历数组 element是每个元素
const a = arr.find(element => element > 3);
console.log(a);//输出4
</script>
5.findIndex 返回第一个满足条件的索引,否则返回-1
<script>
var arr = [1, 2, 3, 4, 5];
// 遍历数组 element是每个元素
const a = arr.findIndex(element => element > 3);
console.log(a);//输出3
</script>
6.filter()所有满足条件的元素组成一个新数组返回,否则为空数组
<script>
var arr = [1, 2, 3, 4, 5];
// 遍历数组 element是每个元素
const a = arr.filter(element => element > 3);
console.log(a);
</script>
7.Object.keys(obj) 可以遍历对象,也可以遍历数组
<script>
var arr = [1, 'a', 3, 4, 5];
var obj = { a: 1, b: 2 }
// 遍历数组返回每个元素的索引
let a = Object.keys(arr);
console.log(a);
// 遍历对象把对象中的每个属性转换成字符组成新数组返回
let b = Object.keys(obj);
console.log(b);
</script>
边栏推荐
猜你喜欢
Easier to use C language entry-level cheese (3) common keywords +define+ pointer + structure (super detailed)
C language to achieve basic version of mine sweeping
el-transfer 左侧右侧默认数据展示
Easier to use C language entry-level cheese (2) select statements + loop statements, functions, arrays, operators (super detailed)
83 reuse of local components [parent to child]
Easier to use C language entry-level cheese (1) data types, variables and constants, strings + escape characters + comments (super detailed)
[learning notes] operation summary of "STL speech contest process management system"
1. Realize MVVM bidirectional data binding stepping pit - infinite trigger get, set
Easily master the library functions related to C language string and memory|
69 simple chat dialog - expand - recall function
随机推荐
C language program environment and preprocessing
Special topic of structure
ES6之Object.defineProperty 和 Proxy 区别
TS 类 class
如何解决跨域问题
8.事件处理——事件修饰符
C语言实现三子棋
ES5新增方法
学习日记3-数据的输入输出
Study diary 1
TS 联合类型
Using C to realize three versions of address book
54-Object. Defineproperty method
Process to thread and incoming thread communication
How is data stored in memory?
【测试技术-自动化测试-pytest】Pytest基础总结
92-兄弟组件间的传值问题
The wonderful use of enum and the cleverness of the union to save space
深拷贝和浅拷贝
c语言自定义类型:结构体、枚举、联合