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

155
Vistas
ReactJS Convert json to [name: value:] pair

I have this json object.

[
    {
        "crime": "LARCENY-NON_VEHICLE",
        "count": "23217"
    },
    {
        "crime": "AUTO_THEFT",
        "count": "13675"
    },
    {
        "crime": "LARCENY-FROM_VEHICLE",
        "count": "28627"
    },
    {
        "crime": "BURGLARY-RESIDENCE",
        "count": "16312"
    }
]

I need to display this data in a pie chart so I need to convert it to this format.

[
    {name: "LARCENY-NON_VEHICLE", value: 23217},
    {name: "AUTO_THEFT", value: 13675},
    {name: "LARCENY-FROM_VEHICLE", value: 28627},
    {name: "BURGLARY-RESIDENCE", value: 16312}
    
]

This is how Im retrieving the data using axios.

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can just use map to return a new array with values from old array

const data = [{
    "crime": "LARCENY-NON_VEHICLE",
    "count": "23217"
  },
  {
    "crime": "AUTO_THEFT",
    "count": "13675"
  },
  {
    "crime": "LARCENY-FROM_VEHICLE",
    "count": "28627"
  },
  {
    "crime": "BURGLARY-RESIDENCE",
    "count": "16312"
  }
];

const newData = data.map(item => {
  return {
    name: item.crime,
    value: +item.count // + to convert string to number
  }
});

console.log(newData)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can simply format an array by calling map function

const arr = [
  {
    crime: "LARCENY-NON_VEHICLE",
    count: "23217",
  },
  {
    crime: "AUTO_THEFT",
    count: "13675",
  },
  {
    crime: "LARCENY-FROM_VEHICLE",
    count: "28627",
  },
  {
    crime: "BURGLARY-RESIDENCE",
    count: "16312",
  },
];

arr.map((e) => ({
  name: e.crime,
  count: Number.parseInt(e.count)
}))
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