在Java中,可以使用for循环遍历数组中的对象,如下所示:
String[] myStringArray = {"Hello", "World"};
for (String s : myStringArray) {
// Do something
}
我可以在JavaScript中做同样的事情吗?
在Java中,可以使用for循环遍历数组中的对象,如下所示:
String[] myStringArray = {"Hello", "World"};
for (String s : myStringArray) {
// Do something
}
我可以在JavaScript中做同样的事情吗?
当前回答
有一种方法可以在循环中的隐式作用域很小的情况下实现,并去掉额外的变量。
var i = 0,
item;
// Note this is weak to sparse arrays or falsey values
for ( ; item = myStringArray[i++] ; ){
item; // This is the string at the index.
}
或者如果你真的想得到id并有一个真正经典的for循环:
var i = 0,
len = myStringArray.length; // Cache the length
for ( ; i < len ; i++ ){
myStringArray[i]; // Don't use this if you plan on changing the length of the array
}
现代浏览器都支持Array原型上的Each、map、reduce、filter等迭代器方法。
其他回答
JavaScript中的数组遍历作弊表
给定一个数组,可以通过以下多种方式之一遍历它。
1.经典for loop
const myArray=['Hello','World'];for(设i=0;i<myArray.length;i++){console.log(myArray[i]);}
2.用于。。。属于
const myArray=['Hello','World'];for(myArray的常量项){console.log(项);}
3.Array.prototype.forEach()
const myArray=['Hello','World'];myArray.forEach(项=>{console.log(项);});
4.while循环
const myArray=['Hello','World'];设i=0;while(i<myArray.length){console.log(myArray[i]);i++;}
5.do…while循环
const myArray=['Hello','World'];设i=0;做{console.log(myArray[i]);i++;}而(i<myArray.length);
6.队列样式
const myArray=['Hello','World'];while(myArray.length){console.log(myArray.shift());}
7.堆栈样式
注:该列表在本页中以相反的方式打印。const myArray=['Hello','World'];while(myArray.length){console.log(myArray.pop());}
最优雅快捷的方式
var arr = [1, 2, 3, 1023, 1024];
for (var value; value = arr.pop();) {
value + 1
}
http://jsperf.com/native-loop-performance/8
编辑(因为我错了)
比较循环遍历100000个项目数组的方法,并每次使用新值执行最小操作。
http://jsben.ch/#/BQhED
准备工作:
<script src="//code.jquery.com/jquery-2.1.0.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>
<script>
Benchmark.prototype.setup = function() {
// Fake function with minimal action on the value
var tmp = 0;
var process = function(value) {
tmp = value; // Hold a reference to the variable (prevent engine optimisation?)
};
// Declare the test Array
var arr = [];
for (var i = 0; i < 100000; i++)
arr[i] = i;
};
</script>
测验:
<a href="http://jsperf.com/native-loop-performance/16"
title="http://jsperf.com/native-loop-performance/16"
><img src="http://i.imgur.com/YTrO68E.png" title="Hosted by imgur.com" /></a>
我还没有看到这种变化,我个人最喜欢这种变化:
给定一个数组:
var someArray = ["some", "example", "array"];
您可以在不访问长度属性的情况下对其进行循环:
for (var i=0, item; item=someArray[i]; i++) {
// item is "some", then "example", then "array"
// i is the index of item in the array
alert("someArray[" + i + "]: " + item);
}
请参阅本JsFidle演示:http://jsfiddle.net/prvzk/
这只适用于非稀疏阵列。这意味着数组中的每个索引实际上都有一个值。然而,我发现在实践中我几乎从未在JavaScript中使用过稀疏数组。。。在这种情况下,通常更容易将对象用作映射/哈希表。如果您有一个稀疏数组,并且希望在0上循环。。length-1,您需要for(vari=0;i<someArray.length;++i)构造,但仍需要在循环内使用if来检查当前索引处的元素是否已实际定义。
此外,正如CMS在下面的注释中所提到的,您只能在不包含任何错误值的数组上使用它。示例中的字符串数组是有效的,但如果您有空字符串或0或NaN等数字,则循环将提前中断。在实践中,这对我来说几乎从来都不是问题,但这是一件需要记住的事情,这使得在使用它之前,这是一个需要考虑的循环……这可能会使某些人失去资格:)
我喜欢这个循环的地方是:
写起来很短无需访问(更不用说缓存)长度属性要访问的项在循环中自动定义你选择的名字下的身体。与array.push和array.please非常自然地结合,使用列表/堆栈等数组
这种方法之所以有效,是因为数组规范规定,当您从索引中读取一个项>=数组长度时,它将返回undefined。当你写到这样的位置时,它实际上会更新长度。
对我来说,这个构造最接近于我喜欢的Java5语法:
for (String item : someArray) {
}
…还有一个额外的好处,那就是了解循环内的当前索引
最好使用顺序for循环:
for (var i = 0; i < myStringArray.length; i++) {
// Do something
}
正式的(也许是旧的)方式是Array.protocol.forEach(…):
var arr = ["apple", "banana", "cherry", "mango"];
arr.forEach(function(item, index, _) {
console.log("[" + index + "] = '" + item + "'");
});