Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

173
Views
Transforming the array of object for Highcharts input?

I am really struggling with the Object transformation. I have an array of Object and want to transform it into Highcharts multi line chart input. I want to get the unique dates first sorted from low to high, which will go into x axis and then transform the Original based on ID and date. The length for each ID.data is going to remain same (for whatever date count is not available for that date it will come as null)

Original:

 [
            {
                "date": "1997-09-29",
                "Count": 100,
                "ID": "AB12-R"
            },
            {
                "date": "1997-12-30",
                "Count": 104.7,
                "ID": "AB13-R"
            },
            {
                "date": "1998-03-30",
                "Count": 981,
                "ID": "BA12-R"
            },
            {
                "date": "1998-06-01",
                "Count": 341,
                "ID": "BA12-R"
            }
        ]

Transformed:

[{
Identiy : 'AB12-R',
data : [100,null,null,null]
},
{
Identiy : 'AB13-R',
data : [null,104.7,null,null]
},{
Identiy : 'BA12-R',
data : [null,null,981,341]
}]

I have tried with reduce but nothing is working it seems. I am able to group it by ID but not able to handle the null and missing count, Can someone please help me here ?

This is what i have tried:

  const result = Original.reduce(function (r, a) {
    r[a.ID] = r[a.ID] || [];
    r[a.ID].push(a);
    return r;
  }, Object.create(null));
  console.log({'Transformed':result})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Take a look at this solution:

const hcData = [];

data.forEach((d, i) => {

  const checkIfExist = hcData.find(data => data.id === d["ID"])

  if (checkIfExist) {
    checkIfExist.data[i] = d["Count"];
  } else {
    const initialData = [...Array(data.length)]
    initialData.fill(null, 0, data.length)

    initialData[i] = d["Count"];
    hcData.push({
      data: initialData,
      id: d["ID"]
    })
  }
})

Demo: https://jsfiddle.net/BlackLabel/k2dg1wns/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!