我从API接收到一个JSON响应,我想将该JSON转换为另一个JSON数组。我收到的响应是:
0: {date: "dailylogs20190116", nettype: "GSM On-Net", total: 143423725}
1: {date: "dailylogs20190116", nettype: "Off-Net", total: 212679126}
2: {date: "dailylogs20190116", nettype: "Int'l", total: 4066210}
3: {date: "dailylogs20190116", nettype: "Landline", total: 6462665}
4: {date: "dailylogs20190116", nettype: "On-Net", total: 31375}
5: {date: "dailylogs20190116", nettype: "Free", total: 0}
6: {date: "dailylogs20190116", nettype: "Premium", total: 16020}
7: {date: "dailylogs20190117", nettype: "Landline", total: 7845098}
8: {date: "dailylogs20190117", nettype: "Off-Net", total: 201740308}
9: {date: "dailylogs20190117", nettype: "GSM On-Net", total: 143701795}
10: {date: "dailylogs20190117", nettype: "Premium", total: 472590}
11: {date: "dailylogs20190117", nettype: "Int'l", total: 4628890}
12: {date: "dailylogs20190117", nettype: "On-Net", total: 24000}
13: {date: "dailylogs20190117", nettype: "Free", total: 0}
14: {date: "dailylogs20190115", nettype: "Off-Net", total: 207502381}
15: {date: "dailylogs20190115", nettype: "GSM On-Net", total: 150536440}
16: {date: "dailylogs20190115", nettype: "Int'l", total: 4859440}
17: {date: "dailylogs20190115", nettype: "Landline", total: 5838292}
18: {date: "dailylogs20190115", nettype: "On-Net", total: 34500}
19: {date: "dailylogs20190115", nettype: "Free", total: 0}
现在我希望得到一个如下格式的JSON。
{
dailylogs20190116: {
nettype: ["GSM ON Net", "Offnet", "int'l", "Landline", "On-Net", "Free", "Premium"],
total: [143423725, 212679126, 4066210, 6462665, 31375, 0]
}
//...and so on
}
我尝试过
let final_json = {};
res.forEach(element => {
for (i = 0; i < this.logs.length; i++) {
if (element.date == this.logs[i]) {
final_json[element.date] = {
"net": element.nettype,
"total": element.total
}
}
}
})
我在这段代码中遇到的问题是,我只能获得每个日期的最后一条记录,例如对于dailylogs20190116,我只能获得Free和total 0。