javascript - 为什么es6中的forEach不可以这么写??
本文介绍了javascript - 为什么es6中的forEach不可以这么写??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
map与forEach, 为什么第3个无效
let arr0=[1,2,3,5,0,9,1000,294,85,3850];
console.log(arr0.map(x=>x+x));
arr0.map(x=>console.log(x+x));
console.log(arr0.forEach(x=>x+x)); // ??
arr0.forEach(x=>console.log(x+x));
怎样才能把forEach的值作为一个数组输出呢??
解决方案
好吧,其实x=>x+x是个简写,等价于x=>{return x+x;};forEach里return会终止遍历的
这篇关于javascript - 为什么es6中的forEach不可以这么写??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文