我正在接收以下格式的API JSON数据,并尝试对其进行修改,以便首先按start_date然后按phys_id对其进行分组。我们正在尝试显示约会可用的每个日期,然后显示每个提供者的可用性。
"Today"
Provider Name #1
2:20pm
2:40pm
Provider Name #2
1:00pm
2:40pm
"Tomorrow"
Provider Name #1
3:20pm
4:40pm
Provider Name #2
12:00pm
1:40pm
接收方式如下:
results: [
{
start_date: "20191119",
begintime: "1220",
appResourceId: "DAD6C44B-3CAC-4BDA-A485-F4BB033FA928",
appLocationId: "14A1B866-8393-4510-8144-A45B5C4FD07D",
phys_id: "035B4177-EF4C-492F-86C8-7157B034DB4A"
},
{
start_date: "20191119",
begintime: "1400",
appResourceId: "DAD6C44B-3CAC-4BDA-A485-F4BB033FA928",
appLocationId: "14A1B866-8393-4510-8144-A45B5C4FD07D",
phys_id: "035B4177-EF4C-492F-86C8-7157B034DB4A"
},
{
start_date: "20191119",
begintime: "1420",
appResourceId: "DAD6C44B-3CAC-4BDA-A485-F4BB033FA928",
appLocationId: "14A1B866-8393-4510-8144-A45B5C4FD07D",
phys_id: "1ACE5431-2771-40C6-B438-DC8E05701600"
},
{
start_date: "20191119",
begintime: "1420",
appResourceId: "DAD6C44B-3CAC-4BDA-A485-F4BB033FA928",
appLocationId: "14A1B866-8393-4510-8144-A45B5C4FD07D",
phys_id: "035B4177-EF4C-492F-86C8-7157B034DB4A"
},
{
start_date: "20191120",
begintime: "1440",
appResourceId: "DAD6C44B-3CAC-4BDA-A485-F4BB033FA928",
appLocationId: "14A1B866-8393-4510-8144-A45B5C4FD07D",
phys_id: "035B4177-EF4C-492F-86C8-7157B034DB4A"
},
{
start_date: "20191120",
begintime: "1600",
appResourceId: "DB37F253-BA17-4672-8A81-B27388EE57DF",
appLocationId: "14A1B866-8393-4510-8144-A45B5C4FD07D",
phys_id: "1ACE5431-2771-40C6-B438-DC8E05701600"
},
{
start_date: "20191120",
begintime: "1640",
appResourceId: "DB37F253-BA17-4672-8A81-B27388EE57DF",
appLocationId: "14A1B866-8393-4510-8144-A45B5C4FD07D",
phys_id: "1ACE5431-2771-40C6-B438-DC8E05701600"
}
]
我正在使用以下代码将其按start_date分组:
let group = this.results.reduce((r, a) => {
r[a.start_date] = [...(r[a.start_date] || []), a];
return r;
}, {});
return group;
然后让我更接近:
{
"20191119": [
{
"start_date": "20191119",
"begintime": "1220",
"appResourceId": "DAD6C44B-3CAC-4BDA-A485-F4BB033FA928",
"appLocationId": "14A1B866-8393-4510-8144-A45B5C4FD07D",
"phys_id": "035B4177-EF4C-492F-86C8-7157B034DB4A"
},
{
"start_date": "20191119",
"begintime": "1400",
"appResourceId": "DAD6C44B-3CAC-4BDA-A485-F4BB033FA928",
"appLocationId": "14A1B866-8393-4510-8144-A45B5C4FD07D",
"phys_id": "035B4177-EF4C-492F-86C8-7157B034DB4A"
},
{
"start_date": "20191119",
"begintime": "1420",
"appResourceId": "DAD6C44B-3CAC-4BDA-A485-F4BB033FA928",
"appLocationId": "14A1B866-8393-4510-8144-A45B5C4FD07D",
"phys_id": "1ACE5431-2771-40C6-B438-DC8E05701600"
},
{
"start_date": "20191119",
"begintime": "1420",
"appResourceId": "DAD6C44B-3CAC-4BDA-A485-F4BB033FA928",
"appLocationId": "14A1B866-8393-4510-8144-A45B5C4FD07D",
"phys_id": "035B4177-EF4C-492F-86C8-7157B034DB4A"
}
],
"20191120": [
{
"start_date": "20191120",
"begintime": "1440",
"appResourceId": "DAD6C44B-3CAC-4BDA-A485-F4BB033FA928",
"appLocationId": "14A1B866-8393-4510-8144-A45B5C4FD07D",
"phys_id": "035B4177-EF4C-492F-86C8-7157B034DB4A"
},
{
"start_date": "20191120",
"begintime": "1600",
"appResourceId": "DB37F253-BA17-4672-8A81-B27388EE57DF",
"appLocationId": "14A1B866-8393-4510-8144-A45B5C4FD07D",
"phys_id": "1ACE5431-2771-40C6-B438-DC8E05701600"
},
{
"start_date": "20191120",
"begintime": "1640",
"appResourceId": "DB37F253-BA17-4672-8A81-B27388EE57DF",
"appLocationId": "14A1B866-8393-4510-8144-A45B5C4FD07D",
"phys_id": "1ACE5431-2771-40C6-B438-DC8E05701600"
}
]
}
有没有一种方法可以使嵌套数组按phys_id分组,以便每个提供程序显示可用的约会时间?还是我会以错误的方式进行操作?
最佳答案
为了优化它,我建议编写一个循环并使用一个普通对象来存储所需的级别。
这是我的示例:
var data = [
{
start_date: "20191119",
begintime: "1220",
appResourceId: "DAD6C44B-3CAC-4BDA-A485-F4BB033FA928",
appLocationId: "14A1B866-8393-4510-8144-A45B5C4FD07D",
phys_id: "035B4177-EF4C-492F-86C8-7157B034DB4A"
},
{
start_date: "20191119",
begintime: "1400",
appResourceId: "DAD6C44B-3CAC-4BDA-A485-F4BB033FA928",
appLocationId: "14A1B866-8393-4510-8144-A45B5C4FD07D",
phys_id: "035B4177-EF4C-492F-86C8-7157B034DB4A"
},
{
start_date: "20191119",
begintime: "1420",
appResourceId: "DAD6C44B-3CAC-4BDA-A485-F4BB033FA928",
appLocationId: "14A1B866-8393-4510-8144-A45B5C4FD07D",
phys_id: "1ACE5431-2771-40C6-B438-DC8E05701600"
},
{
start_date: "20191119",
begintime: "1420",
appResourceId: "DAD6C44B-3CAC-4BDA-A485-F4BB033FA928",
appLocationId: "14A1B866-8393-4510-8144-A45B5C4FD07D",
phys_id: "035B4177-EF4C-492F-86C8-7157B034DB4A"
},
{
start_date: "20191120",
begintime: "1440",
appResourceId: "DAD6C44B-3CAC-4BDA-A485-F4BB033FA928",
appLocationId: "14A1B866-8393-4510-8144-A45B5C4FD07D",
phys_id: "035B4177-EF4C-492F-86C8-7157B034DB4A"
},
{
start_date: "20191120",
begintime: "1600",
appResourceId: "DB37F253-BA17-4672-8A81-B27388EE57DF",
appLocationId: "14A1B866-8393-4510-8144-A45B5C4FD07D",
phys_id: "1ACE5431-2771-40C6-B438-DC8E05701600"
},
{
start_date: "20191120",
begintime: "1640",
appResourceId: "DB37F253-BA17-4672-8A81-B27388EE57DF",
appLocationId: "14A1B866-8393-4510-8144-A45B5C4FD07D",
phys_id: "1ACE5431-2771-40C6-B438-DC8E05701600"
}
]
const group = {};
for (const datum of data) {
const date = datum.start_date;
const pId = datum.phys_id;
if (!group[date]) {
group[date] = {};
}
if (!group[date][pId]) {
group[date][pId] = [];
}
group[date][pId].push(datum);
}
console.log(group);