遍历数组的方法
for循环遍历数组
let arr = [10, 23, 45, 56, 89];
for(let i = 0; i < arr.length;i++){
console.log(i,arr[i]);
}
for-in循环
let arr = [10, 23, 45, 56, 89];
for(let i in arr){
//i 是数组的下标 arr[i] 读取数组中的值
console.log(i,arr[i]);
}
forEach( )遍历数组
说明:forEach()
遍历数组 数组对象的方法 参数是一个函数,函数有俩个参数
let arr = [10, 23, 45, 56, 89];
arr.forEach(function(item,index){
// item 代表数组中的每个值, index 代表数组的索引值
console.log(item,index);
})
map( ) 遍历数组
说明:返回值是一个新数组; 执行的操作是把原数组中的每个值按照指定的规则映射到新数组中
let newArr = arr.map(function(item,index){
// item 代表数组中的每个值, index 代表数组的索引值
console.log(item,index);
// 指定映射规则,通过return返回映射后的值
return item * 10;
})
console.log(newArr);
本文作者:秋弦
本文链接:https://www.cnblogs.com/Eamon-18/p/16096651.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步