在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中做同样的事情吗?
当前回答
在JavaScript中,有很多方法可以循环数组。
下面的代码是流行的代码
/**声明输入*/const items=['你好','世界']/**解决方案1。简单适用于*/console.log(解决方案1。简单用于')for(设i=0;i<items.length;i++){console.log(项[i])}console.log()console.log()/**解决方案2。简单的while*/console.log(解决方案2。简单while')设i=0而(i<items.length){console.log(项[i++])}console.log()console.log()/**解决方案3。对于每个*/console.log(解决方案3。“每个”)items.forEach(item=>{console.log(项)})console.log()console.log()/**解决方案4。的*/console.log(解决方案4。对于')for(常量项,共项){console.log(项)}console.log()console.log()
其他回答
使用while循环。。。
var i = 0, item, items = ['one', 'two', 'three'];
while(item = items[i++]){
console.log(item);
}
它记录:“一”、“二”和“三”
对于相反的顺序,一个更有效的循环:
var items = ['one', 'two', 'three'], i = items.length;
while(i--){
console.log(items[i]);
}
它记录:“三”、“二”和“一”
或者经典的for循环:
var items = ['one', 'two', 'three']
for(var i=0, l = items.length; i < l; i++){
console.log(items[i]);
}
它记录:“一”、“两”、“三”
参考资料:谷歌闭包:如何不编写JavaScript
在JavaScript中,有很多方法可以循环数组。
下面的代码是流行的代码
/**声明输入*/const items=['你好','世界']/**解决方案1。简单适用于*/console.log(解决方案1。简单用于')for(设i=0;i<items.length;i++){console.log(项[i])}console.log()console.log()/**解决方案2。简单的while*/console.log(解决方案2。简单while')设i=0而(i<items.length){console.log(项[i++])}console.log()console.log()/**解决方案3。对于每个*/console.log(解决方案3。“每个”)items.forEach(item=>{console.log(项)})console.log()console.log()/**解决方案4。的*/console.log(解决方案4。对于')for(常量项,共项){console.log(项)}console.log()console.log()
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;
})();
考虑一下:
const ITEMS = ['One','Two','Three']
let item=-1
//Then, you get looping with every call of:
ITEMS[item=item==ITEMS.length-1?0:item+1]
介绍
从大学时代开始,我就用Java、JavaScript、Pascal、ABAP、PHP、Progress 4GL、C/C++以及其他一些我现在想不出来的语言编程。
虽然它们都有自己的语言特点,但每种语言都有许多相同的基本概念。这些概念包括过程/函数、IF语句、FOR循环和WHILE循环。
传统的for循环
传统的for循环有三个组成部分:
初始化:在第一次执行look块之前执行条件:每次执行循环块之前检查一个条件,如果为false,则退出循环事后思考:每次执行循环块后执行
这三个组件通过一个;象征这三个组件中每一个的内容都是可选的,这意味着以下内容是可能的最小循环:
for (;;) {
// Do stuff
}
当然,您需要在for循环中的某处包含if(条件==true){break;}或if(条件===true){return;},以使其停止运行。
不过,通常情况下,初始化用于声明索引,条件用于将该索引与最小值或最大值进行比较,而事后考虑用于增加索引:
for (var i = 0, length = 10; i < length; i++) {
console.log(i);
}
使用传统的for循环遍历数组
循环遍历数组的传统方法是:
for (var i = 0, length = myArray.length; i < length; i++) {
console.log(myArray[i]);
}
或者,如果您喜欢向后循环,请执行以下操作:
for (var i = myArray.length - 1; i > -1; i--) {
console.log(myArray[i]);
}
然而,也有许多可能的变化,例如:
for (var key = 0, value = myArray[key], length = myArray.length; key < length; value = myArray[++key]) {
console.log(value);
}
…或者这个。。。
var i = 0, length = myArray.length;
for (; i < length;) {
console.log(myArray[i]);
i++;
}
…或这个:
var key = 0, value;
for (; value = myArray[key++];){
console.log(value);
}
无论哪种方法最有效,很大程度上都取决于个人品味和您正在实现的具体用例。
请注意,所有浏览器都支持这些变体,包括非常旧的浏览器!
while循环
for循环的一种替代方法是while循环。要循环遍历数组,可以执行以下操作:
var key = 0;
while(value = myArray[key++]){
console.log(value);
}
与传统的for循环一样,即使是最古老的浏览器也支持循环。
此外,请注意,everywhile循环可以重写为for循环。例如,上面的while循环的行为与For循环完全相同:
for(var key = 0; value = myArray[key++];){
console.log(value);
}
对于为。。。属于
在JavaScript中,您也可以这样做:
for (i in myArray) {
console.log(myArray[i]);
}
然而,这应该谨慎使用,因为它在所有情况下的行为都与传统的for循环不同,并且需要考虑潜在的副作用。请参见为什么对数组迭代使用“for…in”是一个坏主意?了解更多详情。
作为…的替代方案。。。在,现在还有。。。属于以下示例显示了for。。。的循环和for。。。循环中:
var myArray = [3, 5, 7];
myArray.foo = "hello";
for (var i in myArray) {
console.log(i); // logs 0, 1, 2, "foo"
}
for (var i of myArray) {
console.log(i); // logs 3, 5, 7
}
此外,您需要考虑没有版本的Internet Explorer支持。。。(边缘12+有)和。。。中至少需要Internet Explorer 10。
Array.prototype.forEach()
for循环的另一种选择是Array.prototype.forEach(),它使用以下语法:
myArray.forEach(function(value, key, myArray) {
console.log(value);
});
所有现代浏览器以及Internet Explorer 9和更高版本都支持Array.prototype.forEach()。
图书馆
最后,许多实用程序库也有自己的foreach变体。AFAIK,最受欢迎的三个是:
jQuery.each(),在jQuery中:
$.each(myArray, function(key, value) {
console.log(value);
});
_.each(),在Undercore.js中:
_.each(myArray, function(value, key, myArray) {
console.log(value);
});
_.forEach(),在Lodash中:
_.forEach(myArray, function(value, key) {
console.log(value);
});