Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda