Is there a way to use map() on an array in reverse order with javascript?

后端 未结 10 1040
离开以前
离开以前 2020-12-23 15:37

I want to use the map() function on a javascript array, but I would like it to operate in reverse order.

The reason is, I\'m rendering stacked React co

10条回答
  •  误落风尘
    2020-12-23 16:16

    With Named callback function

    const items = [1, 2, 3]; 
    const reversedItems = items.map(function iterateItems(item) {
      return item; // or any logic you want to perform
    }).reverse();
    

    Shorthand (without named callback function) - Arrow Syntax, ES6

    const items = [1, 2, 3];
    const reversedItems = items.map(item => item).reverse();
    

    Here is the result

提交回复
热议问题