-1

I have an arrow of dynamically generated data. The format I have is:

const myRow = [
[{ id: 0},{title: "Task 1"}, { complete: 20}],
[{ id: 1},{title: "Task 2"}, { complete: 40}],
[{ id: 2},{title: "Task 3"}, { complete: 40}],

]

Desired row data structure is

const rows = [
  { id: 0, title: "Task 1", complete: 20 ,completed: 690},
  { id: 1, title: "Task 2", complete: 40 ,completed: 690},
  { id: 2, title: "Task 5", complete: 60 ,completed: 690},
  { id: 3, title: "Task 3", complete: 690 ,completed: 690}
];

Most of the solutions I have tried just turns the value to strings which doesn't work for me

Ola John Ajiboye
  • 123
  • 1
  • 1
  • 9
  • You're getting results for turning them to strings because of the keywords "comma separated" which aren't actually relevant here - you just want to flatten an array of objects into one object with all their properties – Klaycon Nov 14 '19 at 17:58
  • Why don't you fix the code that's creating the original data so it puts things into a single object rather than an array? – Barmar Nov 14 '19 at 17:59

1 Answers1

0

Iterate with Array.map(), and spread each array into Object.assign().

Note: I'm not sure where completed came from.

const myRow = [[{"id":0},{"title":"Task 1"},{"complete":20}],[{"id":1},{"title":"Task 2"},{"complete":40}],[{"id":2},{"title":"Task 3"},{"complete":40}]]

const result = myRow.map(arr => Object.assign({}, ...arr))

console.log(result)
Ori Drori
  • 183,571
  • 29
  • 224
  • 209