JavaScript数组迭代 - MDN示例 [英] JavaScript array iteration - MDN example

查看:194
本文介绍了JavaScript数组迭代 - MDN示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  

在MDN网站上阅读JavaScript的重新介绍, for(var i = 0,item; item = a [i ++];){
//用
做一些事情}

其中a []是一个正在循环的数组。

我对item将在第一次迭代。当i = 0且item最初是未定义的,那么当它被赋值为[i ++]时,迭代不会从i = 1开始,这意味着迭代将从a [ ]数组 - > a [1],完全跳过第一个元素a [0]?

解决方案

i ++ 是post 增量操作符,这意味着它通过 1增加 i / code>但计算为旧(非递增)值。

 > i = 0 
0
> i ++
0
>我
1


I was reading the re-introduction to JavaScript on the MDN website and came across this example in the Array section:

for (var i = 0, item; item = a[i++];){
   // Do something with item
}

Where "a[]" is an array being looped over.

I am confused about the value that "item" will have in its first iteration. As i=0 and item is at first undefined, then when it is assigned the value of a[i++] wouldn't the iteration start from i=1, which would mean that the iteration would start from the second element in the a[] array -> a[1], skipping over the first element a[0] entirely?

解决方案

i++ is the post increment operator, which means that it increments i by 1 but evaluates to the old (non-incremented) value.

> i = 0
  0
> i++
  0
> i
  1

这篇关于JavaScript数组迭代 - MDN示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆