在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中的数组遍历作弊表
给定一个数组,可以通过以下多种方式之一遍历它。
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());}
其他回答
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
}
//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)。希望这有所帮助。
使用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中执行同样的操作,但不限于此。在JavaScript中有很多方法可以对数组进行循环。假设下面有一个数组,您想对其进行循环:
var arr = [1, 2, 3, 4, 5];
以下是解决方案:
1) For循环
for循环是JavaScript中循环数组的常见方式,但它不被认为是大型数组的最快解决方案:
for (var i=0, l=arr.length; i<l; i++) {
console.log(arr[i]);
}
2) While循环
while循环被认为是循环长数组的最快方式,但在JavaScript代码中通常很少使用:
let i=0;
while (arr.length>i) {
console.log(arr[i]);
i++;
}
3) 做的同时do-while与while执行相同的操作,但语法差异如下:
let i=0;
do {
console.log(arr[i]);
i++;
}
while (arr.length>i);
这些是实现JavaScript循环的主要方法,但还有其他几种方法可以实现。
此外,我们还使用for in循环对JavaScript中的对象进行循环。
还可以查看JavaScript中Array上的map()、filter()、reduce()等函数。他们做事情可能比使用while和for更快、更好。
如果您想进一步了解JavaScript中的异步函数和数组,这是一篇很好的文章。
函数式编程在当今世界的发展。有充分的理由:功能性技术可以帮助您编写更容易编写的声明性代码一目了然地理解、重构和测试。函数式编程的基石之一是它的特殊用途列表和列表操作。这些东西正是听起来就像是:一系列的东西,以及你对它们做的事情。但功能思维方式对待他们的方式与你有点不同可能会想到。本文将详细介绍我喜欢称之为“大三个“列表操作:map、filter和reduce围绕这三个功能是实现编写干净的功能代码功能和反应式编程的强大技术。这也意味着你再也不用编写for循环了。
阅读更多>>此处:
如果您想使用jQuery,它的文档中有一个很好的示例:
$.each([ 52, 97 ], function( index, value ) {
alert( index + ": " + value );
});