map以及forEach的区别

首先要知道map以及forEach都是用来循环的,然后区别就是map可以又返回值既return,而forEach没有返回值

以下为map和forEach的示例

let arr=[1,2,3,4]
  
  arr.forEach((item,index)=>{
    arr[index] = item+1
  })
  console.log(arr)
 
  arr.map((item,index)=>{
    return index+1
  })
  console.log(arr)

 

 

posted @ 2021-05-13 10:52  帅blog  阅读(187)  评论(0编辑  收藏  举报
/* 鼠标点击求赞文字特效 */