DEV Community


Discussion on: Destructuring arrays as objects

jillejr profile image
Kalle Jillheden

See very little practical use for this but still interesting technique! If I used

let { length, [length-1]: last } = list;

I would just shock all my coworkers and would throw them off, instead of

let length = list.length;
let last = list[length-1];

This technique could really come in handy when need to grab two items out of a list at the same time though. That's a place I'd really consider using it, like with the

let { 0: a, 1: b } = list;
return a > b;

...or whatever. Well got me pondering of how to use it, very interesting. Thanks for sharing! :)

miketalbot profile image
Mike Talbot

To be fair you can do that last example as

 let [a,b] = list
 return a > b

Which is way shorter. It would make sense perhaps for getting items from the end of a list, but as you say not very legible.

jillejr profile image
Kalle Jillheden

Oh yea forgot about that trick! The most interesting thing about this technique is to find where to use it.

Forem Open with the Forem app