catch forEach last iteration

前端 未结 3 978
情话喂你
情话喂你 2021-01-31 00:55
arr = [1,2,3];
arr.forEach(function(i){
// last iteration
});

How to catch when the loop ending? I can do if(i == 3) but I might don\'t kn

3条回答
  •  青春惊慌失措
    2021-01-31 01:20

    const arr= [1, 2, 3]
    arr.forEach(function(element){
     if(arr[arr.length-1] === element){
      console.log("Last Element")
     }
    })
    

提交回复
热议问题