例如:
let arr = [1, 2, 3, 4, 5]
arr.forEach(function(item, index) {
if (index === arr.length - 1) {
console.log('This is the last item!')
}
})
看你需不需要返回值,map返回的是新数组,比如你想让数组成员*2var a = [1, 2, 3, 4, 5]
var b = a.map(function (fn) {
return fn * 2
})
console.log(b)//Array(5) [2, 4, 6, 8, 10]
而for则是遍历每个数组成员