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

后端 未结 10 1022
离开以前
离开以前 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:20

    function mapRevers(reverse) {
        let reversed = reverse.map( (num,index,reverse) => reverse[(reverse.length-1)-index] );
        return reversed;
    }
    
    console.log(mapRevers(myArray));
    

    I You pass the array to map Revers and in the function you return the reversed array. In the map cb you simply take the values with the index counting from 10 (length) down to 1 from the passed array

提交回复
热议问题