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

197
Vistas
How to convert an array from one schema to another in Javascript?

I have a JSON array that looks like this:

[{
   "day":"all",
   "client_first":"3",
   "client_second":"2",
   "client_third":"3"
},
{
   "day":"monday",
   "client_first":"2",
   "client_second":"2",
   "client_third":"2"
}]

I would like to transform the above into the following

[{
    label: 'all',
    data: [3,2,3]
},
{
    label: 'monday',
    data: [2,2,2]
}]

Thanks in advance

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

0

Given input:

const input = [{
   "day":"all",
   "client_first":"3",
   "client_second":"2",
   "client_third":"3"
},
{
   "day":"monday",
   "client_first":"2",
   "client_second":"2",
   "client_third":"2"
}];

Looks like the semantics of what you want to do align very well with Array.map.

For each values in the input array, map it to an object such that label is set by day (key rename), and put in to data the values of the other entries. To get the entries (key,value pairs) we use Object.entries. We Array.filter to get those with key !== 'day'. We want an array of the values, so we map the filtered entries (key,value pairs) to only the values. The input is string, but looks like you want it as numbers, so we convert using Number function

const output = input.map(obj => ({
    label: obj.day,
    data: Object.entries(obj).filter(([key]) => key !== 'day').map(([, val]) => Number(val)),
}));
about 4 years ago · Juan Pablo Isaza Denunciar

0

In one line of code:
arr.map(({day,...clients})=>({label:day,data:[Object.values(clients).map(Number)]})):

  • looping over array and getting the day using Destructuring from object and clients using rest_parameters

let arr = [{
   "day":"all",
   "client_first":"3",
   "client_second":"2",
   "client_third":"3"
},
{
   "day":"monday",
   "client_first":"2",
   "client_second":"2",
   "client_third":"2"
}];
console.log(arr.map(({day,...clients})=>({label:day,data:[Object.values(clients).map(Number)]})))

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