0

Currently, am looping through an Array with the following code:

{allThrows.map((handThrow, index) => {

But this starts with index=0; how can I start and work down to 0 from allThrows.length-1 as the first element and decrement by 1 from there?

4

2 Answers 2

1

there's 2 way:

-reverse()

you can reverse your array first then loop through it by allThrows.reverse().map(), and have in mind, this will reverse the original array. you can store it in a new variable first.

-reduceRight()

this method applies a function on currentValue and send the result (accumulator) to next step, backwards. but you can ignore the passed result and use only currentValue. like this:

allThrows.reduceRight((accumulator,currentValue)=>{ 
console.log(currentValue)
return null
})
Sign up to request clarification or add additional context in comments.

Comments

0

try :

[...allThrows].reverse().map((handThrow, index) => { //do something in reverse }

if you don't care about the index but just about mapping the array items in reverse

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.