在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中做同样的事情吗?
当前回答
Opera、Safari、Firefox和Chrome现在都共享一组增强的Array方法来优化许多常见循环。
您可能不需要所有这些,但它们可能非常有用,或者如果每个浏览器都支持它们,它们就会非常有用。
Mozilla实验室发布了他们和WebKit都使用的算法,因此您可以自己添加它们。
筛选器返回满足某个条件或测试的项目数组。
如果每个数组成员都通过测试,则every返回true。
如果有人通过测试,则返回true。
forEach在每个数组成员上运行一个函数,不返回任何内容。
map类似于forEach,但它返回每个元素的操作结果数组。
这些方法都将一个函数作为其第一个参数,并有一个可选的第二个参数,这是一个对象,当数组成员循环通过该函数时,您希望将其范围强加给数组成员。
忽略它,直到你需要它。
indexOf和lastIndexOf查找与其参数完全匹配的第一个或最后一个元素的适当位置。
(function(){
var p, ap= Array.prototype, p2={
filter: function(fun, scope){
var L= this.length, A= [], i= 0, val;
if(typeof fun== 'function'){
while(i< L){
if(i in this){
val= this[i];
if(fun.call(scope, val, i, this)){
A[A.length]= val;
}
}
++i;
}
}
return A;
},
every: function(fun, scope){
var L= this.length, i= 0;
if(typeof fun== 'function'){
while(i<L){
if(i in this && !fun.call(scope, this[i], i, this))
return false;
++i;
}
return true;
}
return null;
},
forEach: function(fun, scope){
var L= this.length, i= 0;
if(typeof fun== 'function'){
while(i< L){
if(i in this){
fun.call(scope, this[i], i, this);
}
++i;
}
}
return this;
},
indexOf: function(what, i){
i= i || 0;
var L= this.length;
while(i< L){
if(this[i]=== what)
return i;
++i;
}
return -1;
},
lastIndexOf: function(what, i){
var L= this.length;
i= i || L-1;
if(isNaN(i) || i>= L)
i= L-1;
else
if(i< 0) i += L;
while(i> -1){
if(this[i]=== what)
return i;
--i;
}
return -1;
},
map: function(fun, scope){
var L= this.length, A= Array(this.length), i= 0, val;
if(typeof fun== 'function'){
while(i< L){
if(i in this){
A[i]= fun.call(scope, this[i], i, this);
}
++i;
}
return A;
}
},
some: function(fun, scope){
var i= 0, L= this.length;
if(typeof fun== 'function'){
while(i<L){
if(i in this && fun.call(scope, this[i], i, this))
return true;
++i;
}
return false;
}
}
}
for(p in p2){
if(!ap[p])
ap[p]= p2[p];
}
return true;
})();
其他回答
如果有人对Array迭代可用的多种机制的性能方面感兴趣,我准备了以下JSPerf测试:
https://jsperf.com/fastest-array-iterator
结果:
传统的for()迭代器是迄今为止最快的方法,尤其是在缓存数组长度时。
let arr = [1,2,3,4,5];
for(let i=0, size=arr.length; i<size; i++){
// Do something
}
Array.prototype.forEach()和Array.prototype.map()方法是最慢的近似方法,可能是函数调用开销的结果。
//Make array
var array = ["1","2","3","4","5","6","7","8","9","10"]
//Loop
for(var i = 0; i < array.length; i++){
console.log((i+1) + " --> " + array[i])
}
对于i的实际值,如果需要,需要将(i+1)更改为i或(i)。希望这有所帮助。
阵列循环:
for(var i = 0; i < things.length; i++){
var thing = things[i];
console.log(thing);
}
对象循环:
for(var prop in obj){
var propValue = obj[prop];
console.log(propValue);
}
6种不同的方法循环遍历阵列
可以通过许多不同的方法循环遍历数组。我从上到下整理了我最喜欢的6种方法。
1.使用for循环
当它只是在数组中循环时,for循环是我的首选。
让数组=[1,2,3,4,5];for(设i=0;i<array.length;i++){console.log(array[i]);}
2.使用forEach循环
forEach循环是一种在数组中循环的现代方式。此外,它还提供了对阵列和元素的更多灵活性和控制。
让数组=[1,2,3,4,5];array.forEach((元素)=>{console.log(元素);});
3.用于。。。属于
对于of循环允许您直接访问数组元素。
让数组=[1,2,3,4,5];for(数组的let元素){console.log(元素);}
4.用于。。。在回路中
对于中提供了一个键,您可以使用该键访问数组元素。
让数组=[1,2,3,4,5];for(数组中的let索引){console.log(array[index]);}
5.使用while循环
而loop也可以用于循环通过阵列。
让数组=[1,2,3,4,5];let length=array.length;而(长度>0){console.log(array[array.length-length]);长度--;}
6.使用do…while循环
同样,我使用do…while循环
让数组=[1,2,3,4,5];let length=array.length;做{console.log(array[array.length-length]);长度--;}而(长度>0)
for(myStringArray的常量){
(直接回答你的问题:现在你可以了!)
大多数其他答案都是正确的,但他们没有提到(截至本文撰写之时)ECMAScript 6 2015正在为执行迭代带来一种新的机制。。循环的。
这种新语法是在JavaScript中迭代数组的最优雅的方式(只要不需要迭代索引)。
它目前可与Firefox 13+、Chrome 37+一起使用,并且不能与其他浏览器一起使用(请参阅下面的浏览器兼容性)。幸运的是,我们有JavaScript编译器(如Babel),允许我们今天使用下一代功能。
它也适用于Node.js(我在0.12.0版本上测试过它)。
迭代数组
// You could also use "let" or "const" instead of "var" for block scope.
for (var letter of ["a", "b", "c"]) {
console.log(letter);
}
迭代对象数组
const band = [
{firstName : 'John', lastName: 'Lennon'},
{firstName : 'Paul', lastName: 'McCartney'}
];
for(const member of band){
console.log(member.firstName + ' ' + member.lastName);
}
循环发电机:
(示例摘自https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of)
function* fibonacci() { // A generator function
let [prev, curr] = [1, 1];
while (true) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
for (const n of fibonacci()) {
console.log(n);
// Truncate the sequence at 1000
if (n >= 1000) {
break;
}
}
兼容性表:http://kangax.github.io/compat-table/es6/#test-用于。。第页,共页
规范:http://wiki.ecmascript.org/doku.php?id=harmony:iterators
}