1

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?

1 Answer 1

2

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.