Stack Overflow на русском Asked by radion_dev on January 29, 2021
как разделить родительский массив объектов в одном из его значений которого имеется вложенный массив объектов?
в данном случае intervals
let schedule = [
{
"uuid": "1622cbad-ae24-4b5a-aa3d-9e6541386329",
"name": "Проект 1",
"intervals": [
{
"requisition": "0872ed53-74c1-4745-9be7-47885b3f8729",
"num_nodes": 3,
"start_day": 13,
"end_day": 15
}
]
},
{
"uuid": "99ee2597-3afe-47b1-932b-735a435bea78",
"name": "Проект 2",
"intervals": [
{
"requisition": "5a777410-821b-4ff4-bc06-a3fcb9b38eeb",
"num_nodes": 1,
"start_day": 22,
"end_day": 23
},
{
"requisition": "94a55c26-37c1-4f05-a69c-a60cf3dd45e4",
"num_nodes": 1,
"start_day": 15,
"end_day": 16
}
]
},
{
"uuid": "31c4510e-0733-4535-855e-391b3641c945",
"name": "Проект 3",
"intervals": [
{
"requisition": "86ebfa97-a744-41b2-a2cd-38c614f37c30",
"num_nodes": 2,
"start_day": 13,
"end_day": 14
}
]
}
]
и на выходе получить массив такого вида
то есть разбить по количеству интервалов и вернуть в качестве его значение обьект со значениями
let newSchedule = [
{
"uuid": "1622cbad-ae24-4b5a-aa3d-9e6541386329",
"name": "Проект 1",
"intervals":
{
"requisition": "0872ed53-74c1-4745-9be7-47885b3f8729",
"num_nodes": 3,
"start_day": 13,
"end_day": 15
}
},
{
"uuid": "99ee2597-3afe-47b1-932b-735a435bea78",
"name": "Проект 2",
"intervals":
{
"requisition": "5a777410-821b-4ff4-bc06-a3fcb9b38eeb",
"num_nodes": 1,
"start_day": 22,
"end_day": 23
}
},
{
"uuid": "99ee2597-3afe-47b1-932b-735a435bea78",
"name": "Проект 2",
"intervals":
{
"requisition": "94a55c26-37c1-4f05-a69c-a60cf3dd45e4",
"num_nodes": 1,
"start_day": 15,
"end_day": 16
}
},
{
"uuid": "31c4510e-0733-4535-855e-391b3641c945",
"name": "Проект 3",
"intervals":
{
"requisition": "86ebfa97-a744-41b2-a2cd-38c614f37c30",
"num_nodes": 2,
"start_day": 13,
"end_day": 14
}
}
]
const schedule = [
{
uuid: '1622cbad-ae24-4b5a-aa3d-9e6541386329',
name: 'Проект 1',
intervals: [
{
requisition: '0872ed53-74c1-4745-9be7-47885b3f8729',
num_nodes: 3,
start_day: 13,
end_day: 15,
},
],
},
{
uuid: '99ee2597-3afe-47b1-932b-735a435bea78',
name: 'Проект 2',
intervals: [
{
requisition: '5a777410-821b-4ff4-bc06-a3fcb9b38eeb',
num_nodes: 1,
start_day: 22,
end_day: 23,
},
{
requisition: '94a55c26-37c1-4f05-a69c-a60cf3dd45e4',
num_nodes: 1,
start_day: 15,
end_day: 16,
},
],
},
{
uuid: '31c4510e-0733-4535-855e-391b3641c945',
name: 'Проект 3',
intervals: [
{
requisition: '86ebfa97-a744-41b2-a2cd-38c614f37c30',
num_nodes: 2,
start_day: 13,
end_day: 14,
},
],
},
];
console.log(flatSchedules(schedule));
function flatSchedules(schedules) {
return schedules.reduce((acc, schedule) => {
if (schedule.intervals && schedule.intervals .length > 0) {
schedule.intervals.forEach((interval) => {
acc.push({ ...schedule, intervals: interval });
})
} else {
acc.push({ ...schedule });
}
return acc;
}, []);
}
Answered by nörbörnën on January 29, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP