0

I have the following variables:

$firstSection = $main.children[0];
$secondSection = $main.children[1];

To use array destructuring on the first variable, I can do this: [$firstSection] = $main.children;

However, how am I supposed to use array destructuring on the second variable? Thanks.

Ralph David Abernathy
  • 4,879
  • 9
  • 47
  • 72

2 Answers2

1

Just put the second item to destructure to the right of the first, in a comma-separated list. It looks very similar to when declaring an array with 2 items.

const [$firstSection, $secondSection] = $main.children;
CertainPerformance
  • 313,535
  • 40
  • 245
  • 254
1

The values are accessed through a comma separated list, so:

 const [$firstSection, $secondSection] = $main.children; 
 console.log($secondSection); // The second value in the $main.children array

And if you actually don't need the first value in the array, for whatever reason - you can actually just use a comma to omit the first value.

const [, $secondSection] = $main.children;
console.log($secondSection); // The second value in the $main.children array
cmprogram
  • 1,844
  • 2
  • 11
  • 24