Reduce () объект элемента, который уже был уменьшен - PullRequest
0 голосов
/ 25 ноября 2018

Я делал некоторые манипуляции с данными, но я допустил ошибку и получил неправильную структуру данных, поэтому изначально мои данные: ( ссылка на данные)

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 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"}
]

Я спросил, и это дает мне 10 объектов вместо двух, просто нужно поместить этот надоедливый employeeID в 1 объект со всеми начальными значениями в целом.

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

1 Ответ

0 голосов
/ 25 ноября 2018

Поскольку ваши входные и выходные элементы являются однозначными, вы должны использовать .map на employees вместо .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);

Или, если вы хотите создать меньше удаленных промежуточных объектов, вы можете reduce внутренний массив aval вместо .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);
...