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