Javascript - Loop through array backwards with forEach

前端 未结 7 841
醉梦人生
醉梦人生 2020-12-03 17:07

Is there a way to loop backwards through an array using forEach (not any other kind of loop, I know how to do with with a for / standard ways) and without actua

7条回答
  •  死守一世寂寞
    2020-12-03 17:11

    Just use a for loop. Start at the end of the array and go backwards from there.

    const array = ['blastoff', 1, 2, 3];
    
    for (let index = array.length - 1; index >= 0; index--) {
      const element = array[index];
      console.log(element);
    }

提交回复
热议问题