-1
x = { foo: [ { foo: 'foo' } ], bar: [ { bar: 'bar' } ] };

Is it possible to get {foo: 'foo'} and { bar: 'bar' } in one line?

Something like this

({ foo, bar } = x);

would produce [{"foo":"foo"}] [{"bar":"bar"}] which are two arrays and not objects. Of course these arrays can be further destructured, but I'm interested in a one line implementation

1
  • 3
    const { foo: [foo], bar: [bar] } = x Commented Feb 7, 2019 at 15:50

1 Answer 1

0

You can use array destruction to get the first element:

const { foo: [ firstFoo ], bar: [firstBar ]} = x;
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.