35

Is there a simple way to use the spread ... operator to combine an array of objects with a another object to create a single object? This example shows what I'm trying to accomplish:

const arrayOfObjects = [
  { x: 'foo' },
  { y: 'bar' }
];

const obj = {
  hello: 'world'
};

The output I'm looking for is as follows:

{
  x: 'foo',
  y: 'bar',
  hello: 'world'
}

I've tried the following, amongst other things, but it doesn't quite give the intended output.

{
  hello: 'world'
  ...arrayOfObjects
}

// Gives
{
  0: { x: 'foo' },
  1: { y: 'bar' },
  hello: 'world'
};

Is it possible to do this with clever use of the spread operator?

1
  • 1
    Object.assign() would be a very helpful tool for this job. Commented Nov 28, 2016 at 15:52

5 Answers 5

65

You can use Object.assign() with spread syntax ...

const arrayOfObjects = [{
  x: 'foo'
}, {
  y: 'bar'
}];

const obj = {
  hello: 'world'
};

var result = Object.assign({}, obj, ...arrayOfObjects);
console.log(result)

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

3 Comments

Thanks, appears to be the best way to do it. Interesting that there's not a way to do it using nothing but ..., but this works great. Thanks!
Can you clarify why spread is not working here? I thought I knew everything about spread :(
@Genesis Algorithms Well when you spread an array of objects, each individual element is object so you kind of need an additional spread for each object and that is where Object.assign comes in. If you think about it when you just do {...arrayOfObjects} you are trying something like this {{}, {}} and that is invalid syntax.
6

U can use Object.assign();

Object.assign(obj, ...arrayOfObjects)

This option will mutate object obj instead of creating new instance.

1 Comment

Thanks for clarifying that this mutates the object. Note this also works for object properties: let foo = { bar : {} }; Object.assign(foo.bar, ...arrayOfObjects);
3

Object.assign is great, I came up with another possible solution using reduce for who like using it


const arrayOfObjects = [
  { x: 'foo' },
  { y: 'bar' }
];

const obj = {
  hello: 'world'
};

let result = arrayOfObjects.reduce((prev, current) => ({
        ...prev,
        ...current
    }), obj)

console.log(result);
// {hello: "world", x: "foo", y: "bar"}

Comments

3

Based on @Nenad Vracar's answer, there is also this syntax:

{
    ...someObject,
    ...Object.assign({}, ...someArray),
    ...someOtherObject,
}

Seems to work: https://jsfiddle.net/2tq1xem7/

Comments

0

You can use Object.fromEntries()

const arrayOfObjects = [
  { x: 'foo' },
  { y: 'bar' }
];

const obj = {
  hello: 'world'
};

const result = {
  ...Object.fromEntries(arrayOfObjects.flatMap(Object.entries)),
  ...obj,
}

console.log(result)

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.