2025年01月25日 建站教程
js语法中forEach和map哪个写法更简单,下面web建站小编给大家简单介绍一下!
forEach兼容性比较好(所有浏览器)
var arr = [1,2,3,4,5,6,7] arr.forEach(function (val, index, arr){ console.log('index:'+index+','+'val:'+val) });
map是es6语法
var arr = [1,2,3,4,5,6,7] var newArray = arr.map(function (value) { return value+"-1"; }); console.log('输出结果:', newArray) 输出结果: (7) ['1-1', '2-1', '3-1', '4-1', '5-1', '6-1', '7-1']
本文链接:http://so.lmcjl.com/news/21996/