JavaScript ES6: Split array into rest and last with destructuring

Viewed 8398

I just discovered the delightful ES6 destructuring syntax for lists, i.e.

ls = [1, 2, 3]

[first, ...rest] = ls

which sets first to 1 and rest to [2,3]. However, is it possible to split the list into rest=[1,2] and last=3 using similar syntax?

I didn't have any luck googling it. I tried some obvious guesses for such a syntax (see below), but they all produced syntax errors.

[rest..., last] = ls
[...rest, last] = ls

I suppose I could do it by reversing the list twice, so an alternate solution to my question would be a constant time list reversal function.

2 Answers
Related