Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested es6 array destructuring?

Is it possible to nest destructuring?

E.g. I want the first item in an array and in that array I want the first item of that child array.

Given:

let arr = [['foo'], ['bar']];

Is there an easier way to do:

let firstItem = arr[0][0];
like image 843
chrisjlee Avatar asked Jun 03 '16 05:06

chrisjlee


People also ask

Is array Destructuring es6?

Destructuring means to break down a complex structure into simpler parts. With the syntax of destructuring, you can extract smaller fragments from objects and arrays. It can be used for assignments and declaration of a variable.

Can I Destructure nested object?

Destructuring nested objectsIf we need to access an employee's info we can destructure as many levels as it takes to get to our employee object's properties. const { engineers: { 1: { id, name, occupation, }, }, } = employees; Now we have access to all of the second employee object's properties.

How do you Destructure an array of arrays?

Destructuring in Arrays. To destructure an array in JavaScript, we use the square brackets [] to store the variable name which will be assigned to the name of the array storing the element. const [var1, var2, ...]


1 Answers

Ah figured this out:

let [[firstItem]] = arr;

That would be the equivalent.

like image 174
chrisjlee Avatar answered Sep 19 '22 05:09

chrisjlee