Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Destructuring arrays of arrays and getting first element at the same time?

I have a question about array destructuring. I have a data shaped like this :

const sample = [['1', '2,', '3'], ['4', '5,', '6']]

And I was wondering if there was a "one liner" way to get to the first element of the first array, something a little bit like this :

const [ first[0] ] = sample

Right now I'm doing something like this but I find it less "elegant" (I know it's not a problem at all, just trying to expand my knowledge =)

const [ tmp ] = sample
const first = tmp[0]

Thank you in advance for your help !

like image 208
mdcarter Avatar asked Feb 04 '26 08:02

mdcarter


1 Answers

let [[a], [b]] = sample;
console.log(a, b); // "1" "4"