我從 API 中獲取一組物件,格式如下:
[
{
"participant_details":[
{
"participant_id":2,
"participant_name":"Bond James"
},
{
"participant_id":3,
"participant_name":"Barkley Charles"
}
],
"schedule_details":{
"schedule_id":17,
"schedule_name":"bug test",
"job_type":"Registered Nurse",
"schedule_type":"Shared/Grouped Schedule",
"number_of_shifts":10,
"start_date":"2022-05-30 23:00:00",
"end_date":"2022-06-03 06:00:00"
}
},
{
"participant_details":[
{
"participant_id":3,
"participant_name":"Barkley Charles"
}
],
"schedule_details":{
"schedule_id":18,
"schedule_name":"June Chuk Schedule",
"job_type":"Coordinator - Operations",
"schedule_type":"Individual Schedule",
"number_of_shifts":6,
"start_date":"2022-06-02 09:00:00",
"end_date":"2022-06-07 15:00:00"
}
},
{
"participant_details":[
{
"participant_id":2,
"participant_name":"Bond James"
},
{
"participant_id":3,
"participant_name":"Barkley Charles"
}
],
"schedule_details":{
"schedule_id":19,
"schedule_name":"Grouped Re-assignment test",
"job_type":"People & Culture",
"schedule_type":"Shared/Grouped Schedule",
"number_of_shifts":6,
"start_date":"2022-06-04 19:00:00",
"end_date":"2022-06-10 02:00:00"
}
}
]
該屬性participant_details包含有關附加到該屬性中的計劃的參與者的資訊:schedule_details
我的問題是,是否有任何方法可以按參與者對這些資料進行分組,以便最終結果schedule_details按參與者分組到物件陣列中,如下所示:
[
{
"participant_id":2,
"participant_name":"Bond James",
"schedule_details":[
{
"schedule_id":17,
"schedule_name":"bug test",
"job_type":"Registered Nurse",
"schedule_type":"Shared/Grouped Schedule",
"number_of_shifts":10,
"start_date":"2022-05-30 23:00:00",
"end_date":"2022-06-03 06:00:00"
},
{
"schedule_id":19,
"schedule_name":"Grouped Re-assignment test",
"job_type":"People & Culture",
"schedule_type":"Shared/Grouped Schedule",
"number_of_shifts":6,
"start_date":"2022-06-04 19:00:00",
"end_date":"2022-06-10 02:00:00"
}
]
},
{
"participant_id":3,
"participant_name":"Barkley Charles",
"schedule_details":[
{
"schedule_id":17,
"schedule_name":"bug test",
"job_type":"Registered Nurse",
"schedule_type":"Shared/Grouped Schedule",
"number_of_shifts":10,
"start_date":"2022-05-30 23:00:00",
"end_date":"2022-06-03 06:00:00"
},
{
"schedule_id":18,
"schedule_name":"June Chuk Schedule",
"job_type":"Coordinator - Operations",
"schedule_type":"Individual Schedule",
"number_of_shifts":6,
"start_date":"2022-06-02 09:00:00",
"end_date":"2022-06-07 15:00:00"
},
{
"schedule_id":19,
"schedule_name":"Grouped Re-assignment test",
"job_type":"People & Culture",
"schedule_type":"Shared/Grouped Schedule",
"number_of_shifts":6,
"start_date":"2022-06-04 19:00:00",
"end_date":"2022-06-10 02:00:00"
}
]
}
]
uj5u.com熱心網友回復:
您可以通過 Map 中的參與者 ID 鍵入資料,并為每個鍵提供一個包含參與者資訊和空調度陣列的物件。
然后迭代資料以根據參與者 ID 填充這些計劃陣列。
最后從地圖中提取值:
const data = [{"participant_details":[{"participant_id":2,"participant_name":"Bond James"},{"participant_id":3,"participant_name":"Barkley Charles"}],"schedule_details":{"schedule_id":17,"schedule_name":"bug test","job_type":"Registered Nurse","schedule_type":"Shared/Grouped Schedule","number_of_shifts":10,"start_date":"2022-05-30 23:00:00","end_date":"2022-06-03 06:00:00"}},{"participant_details":[{"participant_id":3,"participant_name":"Barkley Charles"}],"schedule_details":{"schedule_id":18,"schedule_name":"June Chuk Schedule","job_type":"Coordinator - Operations","schedule_type":"Individual Schedule","number_of_shifts":6,"start_date":"2022-06-02 09:00:00","end_date":"2022-06-07 15:00:00"}},{"participant_details":[{"participant_id":2,"participant_name":"Bond James"},{"participant_id":3,"participant_name":"Barkley Charles"}],"schedule_details":{"schedule_id":19,"schedule_name":"Grouped Re-assignment test","job_type":"People & Culture","schedule_type":"Shared/Grouped Schedule","number_of_shifts":6,"start_date":"2022-06-04 19:00:00","end_date":"2022-06-10 02:00:00"}}];
const map = new Map(data.flatMap((o) => o.participant_details.map(o =>
[o.participant_id, { ...o, schedule_details: [] }]
)));
for (const o of data) {
for (const {participant_id} of o.participant_details) {
map.get(participant_id).schedule_details.push({...o.schedule_details});
}
}
const result = [...map.values()];
console.log(result);
uj5u.com熱心網友回復:
var x = [
{
"participant_details":[
{
"participant_id":2,
"participant_name":"Bond James"
},
{
"participant_id":3,
"participant_name":"Barkley Charles"
}
],
"schedule_details":{
"schedule_id":17,
"schedule_name":"bug test",
"job_type":"Registered Nurse",
"schedule_type":"Shared/Grouped Schedule",
"number_of_shifts":10,
"start_date":"2022-05-30 23:00:00",
"end_date":"2022-06-03 06:00:00"
}
},
{
"participant_details":[
{
"participant_id":3,
"participant_name":"Barkley Charles"
}
],
"schedule_details":{
"schedule_id":18,
"schedule_name":"June Chuk Schedule",
"job_type":"Coordinator - Operations",
"schedule_type":"Individual Schedule",
"number_of_shifts":6,
"start_date":"2022-06-02 09:00:00",
"end_date":"2022-06-07 15:00:00"
}
},
{
"participant_details":[
{
"participant_id":2,
"participant_name":"Bond James"
},
{
"participant_id":3,
"participant_name":"Barkley Charles"
}
],
"schedule_details":{
"schedule_id":19,
"schedule_name":"Grouped Re-assignment test",
"job_type":"People & Culture",
"schedule_type":"Shared/Grouped Schedule",
"number_of_shifts":6,
"start_date":"2022-06-04 19:00:00",
"end_date":"2022-06-10 02:00:00"
}
}
];
var result = [];
for (const o of x) {
if (o.participant_details.length === 0) {
continue;
}
var newObj = {};
for (const p of o.participant_details) {
var index = result.findIndex((value) => value.participant_id === p.participant_id && value.participant_name === p.participant_name);
if (index !== -1) {
result[index].schedule_details.push(o.schedule_details);
} else {
result.push({
participant_id: p.participant_id,
participant_name: p.participant_name,
schedule_details: [
o.schedule_details
],
});
}
}
}
console.log("%o", result);
uj5u.com熱心網友回復:
您可以使用它Array.reduce()來獲得所需的結果。
我們會在輸入陣列上呼叫 .reduce() 一次,然后在每個輸入的參與者詳細資訊陣列上呼叫一次。
將 schedule_details 分組后,我們將再次轉換為陣列Object.values()
const input = [ { "participant_details":[ { "participant_id":2, "participant_name":"Bond James" }, { "participant_id":3, "participant_name":"Barkley Charles" } ], "schedule_details":{ "schedule_id":17, "schedule_name":"bug test", "job_type":"Registered Nurse", "schedule_type":"Shared/Grouped Schedule", "number_of_shifts":10, "start_date":"2022-05-30 23:00:00", "end_date":"2022-06-03 06:00:00" } }, { "participant_details":[ { "participant_id":3, "participant_name":"Barkley Charles" } ], "schedule_details":{ "schedule_id":18, "schedule_name":"June Chuk Schedule", "job_type":"Coordinator - Operations", "schedule_type":"Individual Schedule", "number_of_shifts":6, "start_date":"2022-06-02 09:00:00", "end_date":"2022-06-07 15:00:00" } }, { "participant_details":[ { "participant_id":2, "participant_name":"Bond James" }, { "participant_id":3, "participant_name":"Barkley Charles" } ], "schedule_details":{ "schedule_id":19, "schedule_name":"Grouped Re-assignment test", "job_type":"People & Culture", "schedule_type":"Shared/Grouped Schedule", "number_of_shifts":6, "start_date":"2022-06-04 19:00:00", "end_date":"2022-06-10 02:00:00" } } ]
const result = Object.values(input.reduce((acc, { participant_details, schedule_details }) => {
return participant_details.reduce((acc, { participant_id, participant_name }) => {
acc[participant_id] = acc[participant_id] || { participant_id, participant_name, schedule_details: [] };
acc[participant_id].schedule_details.push(schedule_details);
return acc;
}, acc);
}, {}))
console.log('Result:', JSON.stringify(result, null, 2));
.as-console-wrapper { max-height: 100% !important; }
uj5u.com熱心網友回復:
干得好:
const input=[{participant_details:[{participant_id:2,participant_name:"Bond James"},{participant_id:3,participant_name:"Barkley Charles"}],schedule_details:{schedule_id:17,schedule_name:"bug test",job_type:"Registered Nurse",schedule_type:"Shared/Grouped Schedule",number_of_shifts:10,start_date:"2022-05-30 23:00:00",end_date:"2022-06-03 06:00:00"}},{participant_details:[{participant_id:3,participant_name:"Barkley Charles"}],schedule_details:{schedule_id:18,schedule_name:"June Chuk Schedule",job_type:"Coordinator - Operations",schedule_type:"Individual Schedule",number_of_shifts:6,start_date:"2022-06-02 09:00:00",end_date:"2022-06-07 15:00:00"}},{participant_details:[{participant_id:2,participant_name:"Bond James"},{participant_id:3,participant_name:"Barkley Charles"}],schedule_details:{schedule_id:19,schedule_name:"Grouped Re-assignment test",job_type:"People & Culture",schedule_type:"Shared/Grouped Schedule",number_of_shifts:6,start_date:"2022-06-04 19:00:00",end_date:"2022-06-10 02:00:00"}}];
console.log(mapResultByParticipant(input));
function mapResultByParticipant(events) {
const participants = {};
events.map((event) => {
//if we don't have such participant - create him
event.participant_details.map((participant) => {
if (!participants[participant.participant_id]) {
participants[participant.participant_id] = {
...participant,
schedule_details: []
}
}
// add event to participant
participants[participant.participant_id].schedule_details.push(event.schedule_details);
});
});
return Object.values(participants);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/491299.html
標籤:javascript 数组
上一篇:將兩個React檔案合二為一
