如何将对象数组中的某些属性合并到另一个数组中?

时间:2019-01-20 19:13:22

标签: javascript arrays

我从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我获得免费且总计0

1 个答案:

答案 0 :(得分:3)

您可以使用简单的reduce获得所需的输出:

const arr = [{date:"dailylogs20190116",nettype:"GSM On-Net",total:143423725},{date:"dailylogs20190116",nettype:"Off-Net",total:212679126},{date:"dailylogs20190116",nettype:"Int'l",total:4066210},{date:"dailylogs20190116",nettype:"Landline",total:6462665},{date:"dailylogs20190116",nettype:"On-Net",total:31375},{date:"dailylogs20190116",nettype:"Free",total:0},{date:"dailylogs20190116",nettype:"Premium",total:16020},{date:"dailylogs20190117",nettype:"Landline",total:7845098},{date:"dailylogs20190117",nettype:"Off-Net",total:201740308},{date:"dailylogs20190117",nettype:"GSM On-Net",total:143701795},{date:"dailylogs20190117",nettype:"Premium",total:472590},{date:"dailylogs20190117",nettype:"Int'l",total:4628890},{date:"dailylogs20190117",nettype:"On-Net",total:24000},{date:"dailylogs20190117",nettype:"Free",total:0},{date:"dailylogs20190115",nettype:"Off-Net",total:207502381},{date:"dailylogs20190115",nettype:"GSM On-Net",total:150536440},{date:"dailylogs20190115",nettype:"Int'l",total:4859440},{date:"dailylogs20190115",nettype:"Landline",total:5838292},{date:"dailylogs20190115",nettype:"On-Net",total:34500},{date:"dailylogs20190115",nettype:"Free",total:0}]

const output = arr.reduce((acc,{date, nettype,total }) => {
  acc[date] = acc[date] || {nettype: [], total:[]};
  acc[date].nettype.push(nettype);
  acc[date].total.push(total);
  return acc
}, {})

console.log(output)