Composite Javascript Objects with Destructuring

Ole Ersoy
1 min readApr 26, 2020
Image by Elias Sch. from Pixabay

Scenario

We have two objects:

const o1 = {
foo: 'foo',
boo: 'boo'
}
const o2 = {
pityTheFoo: 'pityTheFoo'
}

And we want all the keys and values inside a single object like this:

const o3 = {
foo: 'foo',
boo: 'boo',
pityTheFoo: 'pityTheFoo'
}

Approach

Use the destructuring operator:

const o3 = {
...o1,
...o2
}

Demo

--

--