我如何通过一个序列中的所有输入,使用JavaScript?


当前回答

在 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

其他回答

你可以这样打电话给每个人:

forEach 将在您提供的序列上进行 iterate 并为每个 iteration 将有保持该 iteration 的值的元素. 如果您需要索引,您可以通过 i 作为 forEach 的呼叫回复函数中的第二个参数获得当前指数。

Foreach 基本上是一种高顺序函数,它作为其参数需要另一个函数。

let theArray= [1,3,2];

theArray.forEach((element) => {
  // Use the element of the array
  console.log(element)
}

出口:

1
3
2

你也可以在这样的序列上进行 iterate:

for (let i=0; i<theArray.length; i++) {
  console.log(i); // i will have the value of each index
}


如果你需要一个圆体的工作进行序列(而不是平行),一个等待在圆体将等待承诺安排之前继续。

const a = [“a”,“b”,“c”]; for (let index = a.length - 1; index >= 0; --index) { const element = a[index]; console.log(element); }

(ES2015+) 用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于

Caveat 为主机提供的物品

如果你想在一个序列上旋转,请使用标准的三部分旋转。

for (var i = 0; i < myArray.length; i++) {
    var arrayItem = myArray[i];
}

您可以通过缓存 myArray.length 或向后转向它获得某些性能优化。

Mozilla 文档

/* 获取所有表格 */ document.querySelectorAll(“表格” ).forEach(表格 => { /* 每个表格,添加我们提交的事件 */ form.addEventListener(“提交”,事件 => { event.preventDefault(); // Return false /* 显示它 */ alert(event.target.action); console.log(event.target); }; }; ); <form action="form1.php" > <input type="submit" value="Submit" /> </form

如果你想通过一系列用箭函数的对象旋转:

let arr = [{name:'john', age:50}, {name:'clark', age:19}, {name:'mohan', age:26}]; arr.forEach((person)=>{ console.log('I am ' + person.name + ' and I am ' + person.age + ' old'); })