-2

Input:

[
  {
    15: "3.0",
    20: "6.0",
    34: "4.0,
    Dosis: "First",
    Sex: "Male"
  },
  {
    15: "3.0",
    27: "6.0",
    32: "4.0,
    Dosis: "Second",
    Sex: "Male"
  },
  {
    15: "",
    23: "12.0",
    44: "7.0,
    Dosis: "First",
    Sex: "Female"
  },
  {
    15: "3.0",
    70: "6.0",
    54: "34.0,
    Dosis: "Second",
    Sex: "Female"
  },
]

I am trying to reordening some data, because I need to do a Pie Chart in JavaScript.

I did something but going through many processes and maybe there is a better way to do this

This is my actual data (similar):

I need just the totals of the whole data per dosis and sex. I also need to change the "string number" to number.

Expected Output:

[
  {
    totalFirstMale: 13
    Dosis: "First",
    Sex: "Male"
  },
  {
    totalSecondMale: 13
    Dosis: "Second",
    Sex: "Male"
  },
  {
    totalFirstFemale: 19
    Dosis: "First",
    Sex: "Female"
  },
  {
    totalSecondFemale: 43
    Dosis: "Second",
    Sex: "Female"
  },
]

PD: The object key value means the age, but I just need the total per sex.

The original array contains 4 key sex (male, female, unknown, intersex) with same structure.

I tried something like this:

    const maleData= json.filter(el => el.Sex === 'Male')
    const femaleData= json.filter(el => el.Sex === 'Female')

    const dataMaleFirst = maleData[0]
    const dataMaleSecond = maleData[1]
    const dataFemaleFirst = femaleData[0]
    const dataFemaleSecond = femaleData[1]

    const objValueFirstMale = Object.values(dataMaleFirst).map(el => el * 1)
    const objValueSecondMale = Object.values(dataMaleSecond).map(el => el * 1)

    const objValueFirstFemale = Object.values(dataFemaleFirst).map(el => el * 1)
    const objValueSecondFemale = Object.values(dataFemaleSecond).map(el => el * 1)

    let totalFirstMale = 0
    let totalSecondMale = 0
    let totalFirstFemale = 0
    let totalSecondFemale = 0

    for (let i = 0; i < objValueFirstMale.length || i < objValueSegundasHombre.length; i++) {
        !isNaN(objValueFirstMale[i]) ? totalFirstMale += objValueFirstMale[i] : 0
        !isNaN(objValueSecondMale[i]) ? totalSecondMale += objValueSecondMale[i] : 0
    }

    for (let i = 0; i < objValueFirstFemale.length || i < objValueSecondFemale.length; i++) {
        !isNaN(objValueFirstFemale[i]) ? totalFirstFemale += objValueFirstFemale[i] : 0
        !isNaN(objValueSecondFemale[i]) ? totalSecondFemale += objValueSecondFemale[i] : 0
    }
    
    return {
        firstMaleDosis: totalFirstMale,
        secondMaleDosis: totalSecondMale,
        totalMaleDosis: totalFirstMale + totalSecondMale,
        firstFemaleDosis: totalFirstFemale,
        secondFemaleDosis: totalSecondFemale ,
        totalFemaleDosis: totalFirstFemale + totalSecondFemale 
    }

It Looks terrible I know hahahaha and now I have to add unknown data & intersex data. So it could be problematic to do all this "logic" again >_>.

I would really appreciate your help!

3 Answers3

0

You aren't mention which language are you using. This solution is for javascript.

 const data = [
  {
    15: "3.0",
    20: "6.0",
    34: "4.0",
    Dosis: "First",
    Sex: "Male"
  },
  {
    15: "3.0",
    27: "6.0",
    32: "4.0",
    Dosis: "Second",
    Sex: "Male"
  },
  {
    15: "",
    23: "12.0",
    44: "7.0",
    Dosis: "First",
    Sex: "Female"
  },
  {
    15: "3.0",
    70: "6.0",
    54: "34.0",
    Dosis: "Second",
    Sex: "Female"
  },
]
  const output = data.map(el => {
    const countedKey = Object.keys(el).filter(item => !['Dosis', 
    'Sex'].includes(item));
    let totalCount = 0;
    countedKey.forEach(cnt => {
      if(!isNaN(parseInt(el[cnt]))) totalCount += parseInt(el[cnt]);
    });
    return { [`total${el.Dosis}${el.Sex}`]: totalCount, Dosis: el.Dosis, Sex: 
   el.Sex }
  });

console.log(output);
Saydur Rahman
  • 64
  • 2
  • 4
0

Can try out with this:

const data = [
  {
    15: "3.0",
    20: "6.0",
    34: "4.0",
    Dosis: "First",
    Sex: "Male"
  },
  {
    15: "3.0",
    27: "6.0",
    32: "4.0",
    Dosis: "Second",
    Sex: "Male"
  },
  {
    15: "",
    23: "12.0",
    44: "7.0",
    Dosis: "First",
    Sex: "Female"
  },
  {
    15: "3.0",
    70: "6.0",
    54: "34.0",
    Dosis: "Second",
    Sex: "Female"
  },
];
let output = [];
data.forEach(e => {

  const tempElem = {...e};
  delete tempElem['Dosis'];
  delete tempElem['Sex'];
  output.push({
    Dosis: e['Dosis'],
    Sex: e['Sex'],
    [`total${e.Dosis}${e.Sex}`]: Object.values(tempElem).filter(e => e !== '').reduce((a, b) => parseFloat(a) + parseFloat(b), 0)
  });
});
console.log(output);
Parth Mansata
  • 145
  • 1
  • 7
0

Finally I did this:

let output = data.map(({ Dosis, Sex, ...restOfData }) => {
    const countedKey = Object.keys(restOfData)
    
    let totalCount = 0

    countedKey.forEach(el => {
        totalCount += +restOfData[el]
    })

    return {
        Dosis,
        Sex,
        [`total${Dosis}${Sex}`]: totalCount
    }
})

Thanks you all for to clear my doubts !!