I was working on a problem on Codewars.com and I stumbled upon an interesting solution on StackOverflow. Someone was asking how to reverse an array WITHOUT using .reverse().
Here is one solution someone wrote:
let reverse=a=>[...a].map(a.pop,a)
It helps me to look at it like this:
(but I still don’t completely understand it)
let reverse = (a) => {
return [...a].map(a.pop, a);
};
Original thread on StackOverflow
I have used the spread operator, but not in this situation. Why is it used here?
Why is .pop used without the parentheses? .pop()?
I also see that the person added the original array as an argument. I know that it is optional and can be done, but reading at MDN, I don’t understand why.
I understand the big picture: map returns a new array and we are “popping” off the elements one at a time from the end of the array. This reverses the array. But, I really couldn’t explain this to someone and I would like to understand.
So here’s my question: Would anyone like to “walk me through this” and enlighten me as to what is going on here?