My suggestion is a new possibility to destructure the last item from Arrays/Function parameters like so:
// ARRAYS:
const [first, ...rest] = [1,2,3,4]
// first = 1, rest = [2,3,4], works as usual!
const [first, ...rest, last] = [1,2,3,4]
// -> first = 1, rest = [2,3], last = 4
const [...rest, last] = [1,2,3,4]
// rest = [1,2,3], last = 4
const [...rest, last] = [1,2]
// rest = [1], last = 2
const [...rest, last] = [1]
// rest = [1], last = 1
const [first, ...rest, last] = [1]
// first = 1, rest = [1], last = 1
const [...rest, last] = []
// rest = [], last = undefined
// FUNCTION PARAMS:
const a = (first, ...rest, last) => last
a(1) // -> undefined
a(1,2) // -> undefined
a(1,2,3) // -> 3
a(1,2,3,4) // -> 4
// etc.
It would be rather helpful in many situations I come across frequently. At the time of writing, lodash's last()
function tries to do that.
What do you think?
Kind regards,
Lorenz
EDIT:
I see this idea is already being worked on: GitHub - tc39/proposal-deiter: Double-Ended Iterator and Destructuring.