Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda