for...in/for...each/for...of的区别

简介: for...in/for...each/for...of的区别

for in

针对json数组使用

for in 会便利属性上的每个属性名称,

实例

let array={fname:“Bill”,lname:“Gates”,age:56};
   for(let index in array) {  
        console.log(index,array[index]);  
    };  
    //index是fname、Iname、age
    //array[index]为Bill、Gates、56
let iterable = [3, 5, 7];
iterable.foo = "hello";
for (let i in iterable) {
  console.log(i);  //  0, 1, 2, "foo", 
}

forEach

数组时使用,json数组不行 ,没有返回值

参数唯一的时候,就是值本身。

array.forEach(v=>{  
    console.log(v);  
});
array.forEach(function(v){  
    console.log(v);  
});

参数两个的时候,第一个为元素,第二个为下标

let array = [0222, 2221, 231312, 3, 4, 5]
 array.forEach((item, index) => {
 console.log(item, index);
 });

d6bb7edccfe2d1de0989d141b935ecf.png

第三个参数为当前正操作的数组

let array = [1, 221, 4312, 34, 64, 58]
array.forEach((value, index, array) => {
  console.log(value)    //value为遍历的当前元素
  console.log(index)  //index为当前索引
  console.log(array)  //array为正在操作的数组
  console.log('------------------------------------')
}) 
array . forEach (( value , index , array )=>{
// value 为遍历的当前元素, index 为当前索引, array 为正在操作的数组// do something console . log ( value )
 console . log ( index ) console . log ( array )
 console .1og('----
// thisArg 为执行回调时的 this 
(6)[1,221,4312,34,64,58]
221

for of

在ES6中增加的循环,使用方法简单

let array = ['a', 'b', 'c', 'd', 'e', 'f']
for (let v of array) {
  console.log(v);    //v在这里就是每个元素了,
};
>
 let array =[' a ',
 for ( let v of array ){
 console . log ( v );
' b '," c ", d ',' e "," f "
// v 在这里就是每个元素了,
};
 a 
 b 
 c 
 d 
 e 
 f
 undefined 
let myString = "helloabc"; 
for(let char of myString ) { 
    console.log(char); 
}



目录
相关文章
|
存储 Python
关于“Python”的核心知识点整理大全24-2
关于“Python”的核心知识点整理大全24
137 2
|
应用服务中间件 Windows
Tomcat控制台乱码问题修复
控制台产生乱码的原因是在Tomcat 在输出日志中使用的是 UTF-8 编码,而我们中文的Windows 操作系统使用的是 GBK 编码。由于编码格式不统一,所以出现了乱码。
|
11天前
|
人工智能 JSON 供应链
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
LucianaiB分享零成本畅用JVS Claw教程(学生认证享7个月使用权),并开源GeoMind项目——将JVS改造为科研与产业地理情报可视化AI助手,支持飞书文档解析、地理编码与腾讯地图可视化,助力产业关系图谱构建。
23457 10
畅用7个月无影 JVS Claw |手把手教你把JVS改造成「科研与产业地理情报可视化大师」
|
15天前
|
人工智能 缓存 BI
Claude Code + DeepSeek V4-Pro 真实评测:除了贵,没别的毛病
JeecgBoot AI专题研究 把 Claude Code 接入 DeepSeek V4Pro,跑完 Skills —— OA 审批、大屏、报表、部署 5 大实战场景后的真实体验 ![](https://oscimg.oschina.net/oscnet/up608d34aeb6bafc47f
4940 17
Claude Code + DeepSeek V4-Pro 真实评测:除了贵,没别的毛病
|
16天前
|
人工智能 JSON BI
DeepSeek V4 来了!超越 Claude Sonnet 4.5,赶紧对接 Claude Code 体验一把
JeecgBoot AI专题研究 把 Claude Code 接入 DeepSeek V4Pro 的真实体验与避坑记录 本文记录我将 Claude Code 对接 DeepSeek 最新模型(V4Pro)后的真实体验,测试了 Skills 自动化查询和积木报表 AI 建表两个场景——有惊喜,也踩
5925 14
|
4天前
|
人工智能 缓存 Shell
Claude Code 全攻略:命令大全 + 实战工作流(完整版)
Claude Code 是一款运行在终端环境下的 AI 编码助手,能够直接在项目目录中理解代码结构、编辑文件、执行命令、执行开发计划,并支持持久化记忆、上下文压缩、后台任务、多模型切换等专业能力。对于日常开发、项目维护、快速重构、代码审查等场景,它可以大幅减少手动操作、提升编码效率。本文从常用命令、界面模式、核心指令、记忆机制、图片处理、进阶工作流等维度完整说明,帮助开发者快速上手并稳定使用。
930 1

热门文章

最新文章