[英]How can I get totals from array of objects with string values?
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 .我正在尝试重新排列一些数据,因为我需要在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:预期 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. PD:object 键值表示年龄,但我只需要每个性别的总数。
The original array contains 4 key sex (male, female, unknown, intersex) with same structure.原始数组包含 4 个具有相同结构的关键性别(男性、女性、未知、双性人)。
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!我将衷心感谢您的帮助!
You aren't mention which language are you using.你没有提到你使用的是哪种语言。 This solution is for javascript.此解决方案适用于 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);
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);
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 !!谢谢大家解答我的疑惑!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.