我如何通过一个序列中的所有输入,使用JavaScript?
当前回答
// 使用前端 ECMAScript 6 路径通过 路径 路径 路径 路径 路径 路径 路径 路径 路径 路径 路径 路径 路径 路径 路径 路径 路径 路径 路径 路径
其他回答
如果你想通过一系列用箭函数的对象旋转:
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'); })
如果你想使用 forEach(),它会看起来像 -
theArray.forEach ( element => {
console.log(element);
});
如果你想使用(),它会看起来像 -
for(let idx = 0; idx < theArray.length; idx++){
let element = theArray[idx];
console.log(element);
}
总结:
创建新序列:地图
let arr = [1, 2, 3, 4, 5]; let newArr = arr.map(元素,索引,序列) => {返回元素 * 2; }) console.log(arr); console.log(newArr);
forEach 是一种位于 Array.prototype 的函数,它将作为一个论点采取呼叫回归函数,然后对序列中的每个元素进行此呼叫回归函数,与地图() 函数不同,forEach 函数不会返回任何东西(未定义)。
使用到...可能的地方
async /await support |
Skips non-numeric props | Immutable index | |
---|---|---|---|
for...of |
✅ | ✅ | ✅ |
forEach() |
❌ | ✅ | ✅ |
for...in |
✅ | ❌ | ✅ |
Regular for |
✅ | ✅ | ❌ |
正如上面的表中可以看到的那样,它应该随时随地使用,因为它支持非同步功能,并通过随机修改曲线指数来阻止非数字特性。
合成
const nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (const num of nums) {
/* Do something with num */
}
查看更多例子的参考,链接到规格和与与与与与之间的差异,或者可能检查这个教程,以了解它们如何不同。
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