我如何通过一个序列中的所有输入,使用JavaScript?
当前回答
假设我们有几个主题:
let ddl = new Array();
if (subjects) {
subjects.forEach(function (s) {ddl.push({"id": s.id, "label": s.name});});
}
其他回答
如果你需要一个圆体的工作进行序列(而不是平行),一个等待在圆体将等待承诺安排之前继续。
const a = [“a”,“b”,“c”]; for (let index = a.length - 1; index >= 0; --index) { const element = a[index]; console.log(element); }
(ES2015+) 用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于用于
Caveat 为主机提供的物品
Lambda Syntax 通常不适用于 Internet Explorer 10 或更低版本。
我通常使用
[].forEach.call(arrayName,function(value,index){
console.log("value of the looped element" + value);
console.log("index of the looped element" + index);
});
如果您是一个 jQuery 粉丝,并且已经有一个 jQuery 文件运行,您应该逆转指数和值参数的位置。
$("#ul>li").each(function(**index, value**){
console.log("value of the looped element" + value);
console.log("index of the looped element" + index);
});
一个 forEach 实施(见 jsFiddle):
function forEach(list,callback) {
var length = list.length;
for (var n = 0; n < length; n++) {
callback.call(list[n]);
}
}
var myArray = ['hello','world'];
forEach(
myArray,
function(){
alert(this); // do something
}
);
没有内置的能力打破 forEach. 要停止执行,请使用 Array#some 如下:
[1,2,3].some(function(number) {
return number === 1;
});
這是因為有些回來是真實的,一旦任何的呼叫回來,執行在順序,回來是真實的,短循環的執行的其他。
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