I know that
var a = [12,23,132,12,3];
var [first, ...rest] = a;
will give first = 12 and rest = [23,132,12,3]
What I would like to do is make rest as the first variable. Something like this
var a = [12,23,132,12,3];
var [...rest, last] = a;
which should give me rest = [12,23,132,12] and last = 3
But this is a wrong syntax and I will get an error that will say
SyntaxError: Rest element must be last element
I know that I can achieve this by reversing the array and then destructuring like this
var a = [12,23,132,12,3];
var [last, secondLast, ...rest] = a.reverse();
which will give me last = 3, secondLast = 12 and rest = [132,23,12] and then I would again have to reverse() the rest.
I can also use indexes and directly access the array elements as well. But that is not desired.
My question is, are there any other ways to achieve what I am trying to do (using rest operators ?) ?