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

78
Vistas
Format array of objects

I have this array of objects (data). These are the first to indexes:

0: Object

granularity_time: "total"

granularity_geo: "nation"

location_code: "norge"

border: "2020"

age: "90+"

sex: "female"

year: "1900"

week: "1"

yrwk: "1900-01"

season: "1899/1900"

x: "24"

date: "1900-01-01"

n: "219"

date_of_publishing: "2022-01-12"

    tag_outcome: "death"

1: Object

granularity_time: "total"

granularity_geo: "nation"

location_code: "norge"

border: "2020"

age: "90+"

sex: "male"

year: "1900"

week: "1"

yrwk: "1900-01"

season: "1899/1900"

x: "24"

date: "1900-01-01"

n: "127"

date_of_publishing: "2022-01-12"

tag_outcome: "death"

Its statistics where men and woman in the same age has its own object. To index 0 is for woman age 90+, index 1 for men age 90+. Index 2 is for woman 80+, index 3 men 80+ etc.

I want to format the data so each index holds two objects, for men and woman in the same age.

Something like this:

const newData = [
{
  woman: data[0],
  men: data[1]
},
{
  woman: data[2],
  men: data[3]
},
{
  woman: data[4],
  men: data[5]
},
{
  woman: data[6],
  men: data[7]
},
{
  woman: data[8],
  men: data[9]
},
{
  woman: data[10],
  men: data[11]
},
{
  woman: data[12],
  men: data[13]
}

];

Ive tried to make a function that iterates over the data, but each entry ends up undefined:

const formatData = (data) => {
    const newData = [];

    data?.map((item, i) => {
      i % 2 === 0 ? newData.push({ woman: item[i], men: item[i++] }) : null;
    });

    return newData;
  };

What am I missing here?

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

0

This actually works, but im not sure if its best practice since im just using the map to get i..

const formatData = (data) => {
const newData = [];
data?.map((item, i) => {
  i % 2 === 0
    ? newData.push({ woman: data[i], men: data[i + 1] })
    : null;
});
return newData;

};

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could use Array.from() and Array.map() to create the desired output from your original data array.

The length of the new array will be data.length / 2, and each alternate item will be assigned to either the men or women property of each element in the output array.

const template = { year: 1900, location_code: 'norge' };
// Some array of input data...
const data = Array.from({length: 14}, (v,k) => ({...template, id: k, gender: ( k % 2 === 0 ? 'female':'male' )}));

const result = Array.from( { length: data.length / 2 }, (v, idx) => { 
    return { women: data[2*idx], men: data[2*idx + 1],  };
})

console.log(result)
    
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

Since the output is half of the input, it's really more of a reduce()...

const data = [{ gender: "m", name: "sam" }, { gender: "f", name: "sally" }, { gender: "m", name: "allen" }, { gender: "f", name: "abby" }, { gender: "m", name: "jim" }, { gender: "f", name: "jill" }];

const newData = data.reduce((acc, obj, i) => {
  i % 2 ? acc[acc.length-1].woman = obj : acc.push({ man: obj })
  return acc;
}, []);

console.log(newData)

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