【发布时间】:2019-12-19 08:03:23
【问题描述】:
我只是做了一个编码挑战,我知道如何使用经典的 if-else 语句来解决它,该语句使用没有箭头函数的 forEach 循环。
现在我想知道如何在 forEach 循环中使用 ES6 来实现这一点?
// Create a function that returns the product of all odd integers in an array.
const odds = [ 2, 3, 6, 7, 8 ];
const oddProduct = (arr) => {
arr.forEach(function(element) {
if (element % 2 === 0) {
console.log(element);
}
});
};
oddProduct(odds);
我已经学会了如何为 forEach 循环创建箭头函数,但我不知道如何在 if-else 语句中添加。
const oddProduct = (arr) => {
arr.forEach((element) => console.log(element));
};
另外,如果有人能告诉我使用速记语句的最短方法,我很乐意学习!
【问题讨论】:
标签: javascript ecmascript-6 arrow-functions