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!