我如何通过一个序列中的所有输入,使用JavaScript?
当前回答
根据新更新 ECMAScript 6 (ES6) 和 ECMAScript 2015 的功能,您可以使用以下选项:
对于洛普
for(var i = 0; i < 5; i++){
console.log(i);
}
// Output: 0,1,2,3,4
上一篇:在洛普斯
let obj = {"a":1, "b":2}
for(let k in obj){
console.log(k)
}
// Output: a,b
Array.forEach( )
let array = [1,2,3,4]
array.forEach((x) => {
console.log(x);
})
// Output: 1,2,3,4
為...LOPS
let array = [1,2,3,4]
for(let x of array){
console.log(x);
}
// Output: 1,2,3,4
當LOPS
let x = 0
while(x < 5){
console.log(x)
x++
}
// Output: 1,2,3,4
此分類上一篇: while loops
let x = 0
do{
console.log(x)
x++
}while(x < 5)
// Output: 1,2,3,4
其他回答
jQuery 使用 $.map 的方式:
var data = [1, 2, 3, 4, 5, 6, 7];
var newData = $.map(data, function(element) {
if (element % 2 == 0) {
return element;
}
});
// newData = [2, 4, 6];
我来自Python,我发现这种方式更清晰。
theArray 是 array,例子是 array 的元素:
for (let instance of theArray)
{
console.log("The instance", instance);
}
或
for (instance in theArray)
{
console.log("The instance", instance);
}
与比较:
theArray.forEach(function(instance) {
console.log(instance);
});
但是,在一天结束时,两人都在做同样的事情。
根据新更新 ECMAScript 6 (ES6) 和 ECMAScript 2015 的功能,您可以使用以下选项:
对于洛普
for(var i = 0; i < 5; i++){
console.log(i);
}
// Output: 0,1,2,3,4
上一篇:在洛普斯
let obj = {"a":1, "b":2}
for(let k in obj){
console.log(k)
}
// Output: a,b
Array.forEach( )
let array = [1,2,3,4]
array.forEach((x) => {
console.log(x);
})
// Output: 1,2,3,4
為...LOPS
let array = [1,2,3,4]
for(let x of array){
console.log(x);
}
// Output: 1,2,3,4
當LOPS
let x = 0
while(x < 5){
console.log(x)
x++
}
// Output: 1,2,3,4
此分類上一篇: while loops
let x = 0
do{
console.log(x)
x++
}while(x < 5)
// Output: 1,2,3,4
在 jQuery 中,有三种实施。
var a = [3,2];
$(a).each(function(){console.log(this.valueOf())}); //Method 1
$.each(a, function(){console.log(this.valueOf())}); //Method 2
$.each($(a), function(){console.log(this.valueOf())}); //Method 3
如果你需要一个圆体的工作进行序列(而不是平行),一个等待在圆体将等待承诺安排之前继续。
const a = [“a”,“b”,“c”]; for (let index = a.length - 1; index >= 0; --index) { const element = a[index]; console.log(element); }
(ES2015+) 用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于
Caveat 为主机提供的物品