The difference between array traversal map() and forEach

1: First look at map traversal

handleMap(){
    var arr = [1,2,3]
    var list = arr.map((item=>{
         return item
    }))
    console.log("map遍历",list)
    console.log("map遍历后的原数组",arr)
},

print result

It can be seen that the map() method can generate a corresponding value for each value in the original array, and return a new array, and after returning the new array, the original array will not be changed

2: Look at forEach again

// forEach遍历
        handleForEach(){
            var arr = [1,2,3]
            var list = arr.forEach((item,index)=>{
                return item
            })
            console.log("forEach遍历",list)
        }

 

 It can be seen that forEach will not return a new array

Guess you like

Origin blog.csdn.net/YZ_ZZZ24/article/details/123595091