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

263
Vistas
Convierta y asigne datos a un formato comprensible de gráfico

Quiero convertir estos datos

 var data = [{ "Country": "World", "Source": "Electricity", "Year": "2022", "Value": "20.0000" }, { "Country": "World", "Source": "Gas", "Year": "2022", "Value": "30.0000" }, { "Country": "World", "Source": "Hydrogen", "Year": "2022", "Value": "40.0000" }, { "Country": "World", "Source": "Oil", "Year": "2022", "Value": "50.0000" }, { "Country": "World", "Source": "Renewables", "Year": "2022", "Value": "60.0000" },{ "Country": "World", "Source": "Renewables", "Year": "2021", "Value": "50.0000" } ];

a este formato

 [{ name: 'Renewables', data: [60.0000, 50.0000] }, { name: 'Oil', data: [50.0000] },{ name: 'Hydrogen', data: [40.0000] }, { name: 'Gas', data: [30.0000] }, { name: 'Electricity', data: [20.0000] }]

¿Cómo puedo hacer eso?

Cualquier sugerencia por favor

Código que he hecho

 for (let index = 0; index < uniqueSources.length; index++) { var arrSmall = []; for (let j = 0; j < this.ResponseToShow.length; j++) { if (uniqueSources[index] == this.ResponseToShow[j]['Source']) { arrSmall.push(Number(this.ResponseToShow[j]['Value'])); } } } console.log(arrSmall);
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Caso de uso común de reduce

 var data = [{ "Country": "World", "Source": "Electricity", "Year": "2022", "Value": "20.0000" }, { "Country": "World", "Source": "Gas", "Year": "2022", "Value": "30.0000" }, { "Country": "World", "Source": "Hydrogen", "Year": "2022", "Value": "40.0000" }, { "Country": "World", "Source": "Oil", "Year": "2022", "Value": "50.0000" }, { "Country": "World", "Source": "Renewables", "Year": "2022", "Value": "60.0000" },{ "Country": "World", "Source": "Renewables", "Year": "2021", "Value": "50.0000" } ]; let res = Object.values(data.reduce((acc,curr) => { if(!acc[curr.Source]){ acc[curr.Source] = {name: curr.Source,data: []} } acc[curr.Source]['data'].push(curr.Value) return acc; },{})) console.log(res)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Puede usar .forEach() , .findIndex() y .reverse() para lograr esto. Prueba esto

 var data = [ { "Country": "World", "Source": "Electricity", "Year": "2022", "Value": "20.0000" }, { "Country": "World", "Source": "Gas", "Year": "2022", "Value": "30.0000" }, { "Country": "World", "Source": "Hydrogen", "Year": "2022", "Value": "40.0000" }, { "Country": "World", "Source": "Oil", "Year": "2022", "Value": "50.0000" }, { "Country": "World", "Source": "Renewables", "Year": "2022", "Value": "60.0000" },{ "Country": "World", "Source": "Renewables", "Year": "2021", "Value": "50.0000" } ]; let fdata = []; // formatted data data.forEach(({Source, Value}) => { let index = fdata.findIndex(o => o.name === Source); // check if Source exist if( index === -1 ){ fdata.push({name: Source, data: [Value]}); }else{ fdata[index].data.push(Value); } }); fdata = fdata.reverse(); // to reverse the order console.log(fdata)

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