Is there an easy way to use the spread operator ...to combine an array of objects with another object to create one object? This example shows what I'm trying to do:
const arrayOfObjects = [
{ x: 'foo' },
{ y: 'bar' }
];
const obj = {
hello: 'world'
};
The result I'm looking for is as follows:
{
x: 'foo',
y: 'bar',
hello: 'world'
}
I tried, among other things, the following, but this does not quite give the expected result.
{
hello: 'world'
...arrayOfObjects
}
{
0: { x: 'foo' },
1: { y: 'bar' },
hello: 'world'
};
Can this be done with the smart use of the spread operator?
source
share