brohymn
brohymn

Reputation: 460

Reduce() an object of an element that has already been reduced

I was doing some data manipulation, but I made a mistake and got the wrong data structure, so initially my data is: (link to data)

const employees = [{
    "EmployeeID": "100A",
    "FirstName": "Downs",
    "aval": [
       {"start": "11-19", "end": "2", "ava": "30", "health": "4"},
       {"start": "11-20", "end": "2", "ava": "40", "health": "4"},
       {"start": "11-21", "end": "2", "ava": "50", "health": "4"},
       {"start": "11-22", "end": "2", "ava": "60", "health": "4"}
    ]
  },
  {
    "EmployeeID": "100B",
    "FirstName": "Mckenzie",
    "aval": [
       {"start": "11-19", "end": "2", "ava": "1", "health": "4"},
       {"start": "11-20", "end": "2", "ava": "2", "health": "4"},
       {"start": "11-21", "end": "2", "ava": "3", "health": "4"},
       {"start": "11-22", "end": "2", "ava": "4", "health": "4"}
    ]
  },

]

and would like to get it like:

const employees = [
   { "EmployeeID": "100A", "11-19": "30", "11-20": "40", "11-21": "50", "11-22": "60"},
   { "EmployeeID": "100B", "11-19": "1", "11-20": "2", "11-21": "3", "11-22": "4"}
]

I've asked around and this gives me 10 objects instead of two, just need to put those pesky employeeID inside 1 object with all the start values altogether.

const res = employees.reduce((acc, { EmployeeID, aval}) => [
      ...acc,
      ...aval.map( ({ start, ava}) => ({ EmployeeID, [start]: ava}) )
    ], []);

Upvotes: 4

Views: 66

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370789

Because your input items and output items are one-to-one, you should use .map on employees instead of .reduce:

const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

const res = employees.map(({ EmployeeID, aval }) => (
  Object.assign(
    { EmployeeID },
    ...aval.map(({ start, ava }) => ({ [start]: ava }))
  )
));
console.log(res);

Or, if you wanted to create fewer discarded intermediate objects, you could reduce the inner array aval instead of .map:

const employees=[{"EmployeeID":"100A","FirstName":"Downs","aval":[{"start":"11-19","end":"2","ava":"30","health":"4"},{"start":"11-20","end":"2","ava":"40","health":"4"},{"start":"11-21","end":"2","ava":"50","health":"4"},{"start":"11-22","end":"2","ava":"60","health":"4"}]},{"EmployeeID":"100B","FirstName":"Mckenzie","aval":[{"start":"11-19","end":"2","ava":"1","health":"4"},{"start":"11-20","end":"2","ava":"2","health":"4"},{"start":"11-21","end":"2","ava":"3","health":"4"},{"start":"11-22","end":"2","ava":"4","health":"4"}]},];

const res = employees.map(({ EmployeeID, aval }) => (
  aval.reduce((a, { start, ava }) => {
    a[start] = ava;
    return a;
  }, { EmployeeID })
));
console.log(res);

Upvotes: 3

Related Questions