JorahFriendzone
JorahFriendzone

Reputation: 433

Destructuring within array destructure

Would it be possible to destructure within an array destructuring in the same line?

For example:

const array = [{a: 1, b: 2}, {c: 3, d: 4}]

const [ value1 ] = array;
const { a } = value1;

The following allows me to get the value a, but I was wondering if it would be possible to combine the 2nd and 3rd line together?

Upvotes: 1

Views: 65

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370759

Replace the value1 with { a } - it looks the same as declaring an object literal inside an array, except with destructuring:

const array = [{a: 1, b: 2}, {c: 3, d: 4}]
const [{ a }] = array;
console.log(a);

(that said, I wouldn't recommend writing code like this most of the time, it looks a bit confusing)

Upvotes: 2

Related Questions