Home > Software engineering >  Format array of objects
Format array of objects

Time:01-14

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?

CodePudding user response:

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)

CodePudding user response:

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; }

CodePudding user response:

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;

};

  •  Tags:  
  • Related