3

i am receiving a JSON as a response from an API i want to convert that JSON to another array of JSON the response i am getting is

 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}

Now i want a json in such format

{
    dailylogs20190116: {
        nettype: ["GSM ON Net", "Offnet", "int'l", "Landline", "On-Net", "Free", "Premium"],
        total: [143423725, 212679126, 4066210, 6462665, 31375, 0]
    }
    //...and so on
}

i tried

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
            }
        }
    }
})

the issue i am having with this code is i am only getting last record of each date i.e for dailylogs20190116 i am getting Free and total 0

1
  • @jonrsharpe i tried the following please see the edited question Commented Jan 20, 2019 at 19:16

1 Answer 1

3

You can use a simple reduce to get the desired output:

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)

Sign up to request clarification or add additional context in comments.

2 Comments

sir you are awesome but what is acc?
@BensonOO That's the accumulator parameter. Please go through the link to learn how reduce works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.