在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中做同样的事情吗?
当前回答
您可以使用map,这是一种函数式编程技术,也可以在Python和Haskell等其他语言中使用。
[1,2,3,4].map( function(item) {
alert(item);
})
一般语法为:
array.map(func)
一般来说,func需要一个参数,这是数组的一个项。但在JavaScript的情况下,它可以接受第二个参数,即项目的索引,以及第三个参数,也就是数组本身。
array.map的返回值是另一个数组,因此可以这样使用:
var x = [1,2,3,4].map( function(item) {return item * 10;});
现在x是[10,20,30,40]。
您不必内联编写函数。它可能是一个单独的功能。
var item_processor = function(item) {
// Do something complicated to an item
}
new_list = my_list.map(item_processor);
这相当于:
for (item in my_list) {item_processor(item);}
除非你没有得到new_list。
其他回答
阵列迭代有4种方式:
// 1: for
for (let i = 0; i < arr.length; ++i) {
console.log(arr[i]);
}
// 2: forEach
arr.forEach((v, i) => console.log(v));
// 3: for in
for (let i in arr) {
console.log(arr[i]);
}
// 4: for of
for (const v of arr) {
console.log(v);
}
总结:1和3个解决方案创建额外的变量,2-创建额外的函数上下文。最好的方式是第四位-“for of”。
在JavaScript中,不建议使用for In循环遍历Array,但最好使用for循环,例如:
for(var i=0, len=myArray.length; i < len; i++){}
它也进行了优化(“缓存”数组长度)。如果你想了解更多,请阅读我关于这个主题的帖子。
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;
})();
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());}
简单的单线解决方案:
arr=[“桌子”,“椅子”];//解决方案arr.map((e)=>{控制台日志(e);返回e;});