例如:
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则是遍历每个数组成员
var data = {'h':'hello','w':'world'}你说的map是这种形式的数据吧
for(var i in data){
document.write('key::'+i)
document.write('value::'+data[i])
}
输出 key::h value::hello key::w value::world