Search code examples
javascriptecmascript-6destructuring

One line Destructuring of an object inside an array?


Given an array:

const array = [{a:1}]

Is there a one liner to extract the value of a using destructuring?

I tried something like this which doesn't work:

const [{a} = array]

Solution

  • You could unpack array and then first object inside it to get a.

    const array = [{a:1}]
    const [{a}] = array;
    console.log(a)