Question: There is an object and an array.

```
list: [
  {
    oldData: {
      title: 'abc',
      id: 1,
      date: '1982-09-30',
      budget: 250000,
    },
    newData: [
      {
        key: 1,
        data: null,
        value: 5,
      },
      {
        key: 2,
        data: null,
        value: 22,
      },
      ...
    ],
  },
  {
    oldData: {
      title: 'blablablaaaaa',
      id: 2,
      date: '2012-02-23',
      budget: 350000,
    },
    newData: [
      {
        key: 1,
        data: null,
        value: 35,
      },
      {
        key: 2,
        data: null,
        value: 41,
      },
      ...
    ],
  },
  ... some more datas...
]

```

as above, There is more data of the same type.

I need to use `oldData and newData` together, so I want to combine the two.

How can I combine `oldData and newData` so that there are multiple sets of `oldData and newData pairs`?

for example, `[{ combineData: {...} }, { combineData: {...} }, ... }]` here.

I know how to combine array and array, object and object, but I do not know how to do that.

Is there any good solution?

Answer: 
You can use `map()` on the array. And use `Object.assign()` and spread operator to combine all the properties of all the elements in `newData` into one object.

```js
const arr = [
  {
    oldData: {
      a:10,
      b:20
    },
    newData: [
      {
        c:30,
        d:40
      },
      {
        e:50,
        f:60
      }
    ],
  }
]
const res = arr.map(x => ({combinedData:{...x.oldData, ...Object.assign({}, ...x.newData)}}))

console.log(res)
```
You can map over the array and use object destructuring (`...object`) to create a new, combined object:

```js
const data = [
  {
    oldData: {
      foo: 'lorem',
    },
    newData: {
      bar: 'ipsum',
    },
  },
  {
    oldData: {
      foo: 'dolor',
    },
    newData: {
      bar: 'sit amet',
    },
  },
];

const combined = data.map(record => ({...record.oldData, ...record.newData}));
console.log(combined);
```

This will overwrite duplicate keys however, so something like:

```js
{
  oldData: {
    message: 'a',
  },
  newData: {
    message: 'b',
  },
}

```

will become:

```js
{
  message: 'b',
}

```