0
点赞
收藏
分享

微信扫一扫

JS中数组foreach的使用

蛇发女妖 2022-01-31 阅读 44

JS中数组foreach的使用

foreach()方法需要一个函数作为参数,这个函数被称为回调函数。

什么是回掉函数❓

回掉函数由我们创建,但不由我们调用

foreach的使用:
let arr = ["AOK","JIN","OPE","IOP"];

arr.forEach(function (value, index){
    console.log(value);// 元素值
    console.log(index);// 元素索引下标
})
还可以传递一个array形参值
let arr = ["AOK","JIN","OPE","IOP"];

arr.forEach(function (value, index, array){
    console.log(value+' '+index+' '+array);
})
输出结果:

在这里插入图片描述

举报

相关推荐

0 条评论