javascript - 迭代数组 : another approach

标签 javascript for-loop iteration while-loop

<分区>

令我烦恼的是,您将用于迭代数组的 for 循环称为最佳方法,因为在数组内部您需要手动获取迭代元素。在某些情况下,我找到了另一种更适合我的方法:

var data=['test1', 'test2', 'test3'], item=0, n=0;
while ((item=data[n++])) {
   // here you can use item instead of data[n]
}

问候,LX

最佳答案

如果数组包含 0、false、null 或其他“falsy”值,将失败。

While 会在数组中间退出

关于javascript - 迭代数组 : another approach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5592393/

相关文章:

r - 使用 R 查找总和为给定值的元素

javascript - 如何使用 PhantomJS 保存 AJAX 请求的内容

javascript - NodeJS模块开发的疑惑

java - 如何在 for 语句中放置 if, else 语句

python - python循环中计数器变量放在哪里?

python - numpy 中的多个插入,其中配对元素没有潜台词

arrays - 一次访问数组的一些元素并环绕到开头

javascript - JSON 重定向到\currentmap\jsonobj

javascript - 使用 Javascript 或 PHP 从网页保存图像

java - 在不同方法中重用 FileWriter 时在哪里关闭它?