英文:
Calculate percentages from quantities in an array of objects
问题
genderResults: [
{ Percentage: 47, Gender: 'Male', Answer: 'Badminton' },
{ Percentage: 53, Gender: 'Female', Answer: 'Badminton' },
{ Percentage: 50, Gender: 'Male', Answer: 'None of the above' },
{ Percentage: 50, Gender: 'Female', Answer: 'None of the above' },
{ Percentage: 37, Gender: 'Male', Answer: 'Rugby' },
{ Percentage: 63, Gender: 'Female', Answer: 'Rugby' },
{ Percentage: 44, Gender: 'Male', Answer: 'Soccer' },
{ Percentage: 56, Gender: 'Female', Answer: 'Soccer' },
{ Percentage: 56, Gender: 'Male', Answer: 'Squash' },
{ Percentage: 43, Gender: 'Female', Answer: 'Squash' },
{ Percentage: 100, Gender: 'Other', Answer: 'Squash' },
{ Percentage: 60, Gender: 'Female', Answer: 'Tennis' },
{ Percentage: 40, Gender: 'Male', Answer: 'Tennis' }
]
英文:
I have an array containing quantities of votes by gender and would like to convert the quantities to percentages by gender.
The array is as follows:
genderResults: [
{ Qty: 7, Gender: 'Male', Answer: 'Badminton' },
{ Qty: 8, Gender: 'Female', Answer: 'Badminton' },
{ Qty: 2, Gender: 'Male', Answer: 'None of the above' },
{ Qty: 2, Gender: 'Female', Answer: 'None of the above' },
{ Qty: 3, Gender: 'Female', Answer: 'Rugby' },
{ Qty: 5, Gender: 'Male', Answer: 'Rugby' },
{ Qty: 4, Gender: 'Male', Answer: 'Soccer' },
{ Qty: 3, Gender: 'Female', Answer: 'Soccer' },
{ Qty: 21, Gender: 'Male', Answer: 'Squash' },
{ Qty: 16, Gender: 'Female', Answer: 'Squash' },
{ Qty: 1, Gender: 'Other', Answer: 'Squash' },
{ Qty: 18, Gender: 'Female', Answer: 'Tennis' },
{ Qty: 12, Gender: 'Male', Answer: 'Tennis' }
]
The required result would be like:
genderResults: [
{ Percentage: 47, Gender: 'Male', Answer: 'Badminton' },
{ Percentage: 53, Gender: 'Female', Answer: 'Badminton' },
{ Percentage: 50, Gender: 'Male', Answer: 'None of the above' },
{ Percentage: 50: Gender: 'Female', Answer: 'None of the above' },
{ Percentage: 37, Gender: 'Male', Answer: 'Rugby' },
{ Percentage: 63: Gender: 'Female', Answer: 'Rugby' },
...
]
I've tried a forEach loop and array.map but can't figure out how to group the results?
答案1
得分: 1
const totalQtyByGender = {};
const totalQtyByAnswer = {};
// 计算按性别和答案分类的总数量
genderResults.forEach(result => {
const { Qty, Gender, Answer } = result;
totalQtyByGender[Gender] = (totalQtyByGender[Gender] || 0) + Qty;
totalQtyByAnswer[Answer] = (totalQtyByAnswer[Answer] || 0) + Qty;
});
// 计算百分比
const output = genderResults.map(result => {
const { Qty, Gender, Answer } = result;
const percentage = (Qty / totalQtyByGender[Gender]) * 100;
return { Percentage: percentage, Gender, Answer };
});
console.log(output);
我们也可以使用 reduce 方法来实现,但你已经使用了 forEach 方法,所以这就是对应的代码。
英文:
const totalQtyByGender = {};
const totalQtyByAnswer = {};
// Calculate total quantity by gender and answer
genderResults.forEach(result => {
const { Qty, Gender, Answer } = result;
totalQtyByGender[Gender] = (totalQtyByGender[Gender] || 0) + Qty;
totalQtyByAnswer[Answer] = (totalQtyByAnswer[Answer] || 0) + Qty;
});
// Calculate the percentage
const output = genderResults.map(result => {
const { Qty, Gender, Answer } = result;
const percentage = (Qty / totalQtyByGender[Gender]) * 100;
return { Percentage: percentage, Gender, Answer };
});
console.log(output);
We can also do it with the help of reduce method but you tried it with the help of foreach method so here it is.
答案2
得分: 1
这是代码的翻译部分:
这里是你的代码 :)
更新部分的翻译:
这里是更新后的代码 :)
英文:
It can be cleaned up a bit, but I think this will fulfill your requirements. I haven't considered any edge cases, btw.
const genderResults = [
{ Qty: 7, Gender: 'Male', Answer: 'Badminton' },
{ Qty: 8, Gender: 'Female', Answer: 'Badminton' },
{ Qty: 2, Gender: 'Male', Answer: 'None of the above' },
{ Qty: 2, Gender: 'Female', Answer: 'None of the above' },
{ Qty: 3, Gender: 'Female', Answer: 'Rugby' },
{ Qty: 5, Gender: 'Male', Answer: 'Rugby' },
{ Qty: 4, Gender: 'Male', Answer: 'Soccer' },
{ Qty: 3, Gender: 'Female', Answer: 'Soccer' },
{ Qty: 21, Gender: 'Male', Answer: 'Squash' },
{ Qty: 16, Gender: 'Female', Answer: 'Squash' },
{ Qty: 1, Gender: 'Other', Answer: 'Squash' },
{ Qty: 18, Gender: 'Female', Answer: 'Tennis' },
{ Qty: 12, Gender: 'Male', Answer: 'Tennis' }
];
const totalCount = getCount();
const genderPercent = genderResults.map(res =>{
return {Gender: res.Gender, Answer: res.Answer, Percentage: Math.round((res.Qty / totalCount) * 100) }
});
// const maleCount = getCount('Male');
// const femaleCount = getCount('Female');
function getCount(gender){
return genderResults.reduce((sum, item)=>{
if(gender){
if(item.Gender == gender){
sum+= item.Qty;
}
}
else{
sum+= item.Qty;
}
return sum
},0)
}
console.log(totalCount, genderPercent)
Update:
Here you go
const categoryCount = genderResults.reduce((res, item)=>{
if(res[item.Answer]){
res[item.Answer]+= item.Qty
}
else{
res[item.Answer] = item.Qty
}
return res;
}, {})
console.log({categoryCount})
const genderPercent = genderResults.map(res =>{
return {
Gender: res.Gender,
Answer: res.Answer,
Percentage: Math.round((res.Qty / categoryCount[res.Answer]) * 100)
}
});
console.log(genderPercent)
答案3
得分: 1
我们可以使用Array.reduce()
来按答案对genderResults数组进行分组。
我们还会跟踪每个答案的总数量,并使用这个来计算每个答案的百分比数组。
一旦我们有了这个分组的值,我们可以使用Object.values
和Array.flatMap
来获得最终结果。
const genderResults = [ { Qty: 7, Gender: 'Male', Answer: 'Badminton' }, { Qty: 8, Gender: 'Female', Answer: 'Badminton' }, { Qty: 2, Gender: 'Male', Answer: 'None of the above' }, { Qty: 2, Gender: 'Female', Answer: 'None of the above' }, { Qty: 3, Gender: 'Female', Answer: 'Rugby' }, { Qty: 5, Gender: 'Male', Answer: 'Rugby' }, { Qty: 4, Gender: 'Male', Answer: 'Soccer' }, { Qty: 3, Gender: 'Female', Answer: 'Soccer' }, { Qty: 21, Gender: 'Male', Answer: 'Squash' }, { Qty: 16, Gender: 'Female', Answer: 'Squash' }, { Qty: 1, Gender: 'Other', Answer: 'Squash' }, { Qty: 18, Gender: 'Female', Answer: 'Tennis' }, { Qty: 12, Gender: 'Male', Answer: 'Tennis' } ]
const grouped = genderResults.reduce((acc, row) => {
const key = row.Answer;
acc[key] = acc[key] || { totalQty: 0, entries: [] };
acc[key].entries.push(row);
acc[key].totalQty += row.Qty;
acc[key].percentages = acc[key].entries.map(entry => {
return {
Percentage: Math.round(100 * entry.Qty / acc[key].totalQty),
Gender: entry.Gender,
Answer: entry.Answer
}
})
return acc;
}, {})
const result = Object.values(grouped).flatMap(obj => obj.percentages)
console.log('Result:')
result.forEach(r => console.log(JSON.stringify(r)))
.as-console-wrapper { max-height: 100% !important; }
英文:
We can use Array.reduce()
to group the genderResults array by
Answer.
We'll also keep track of the total quantity for each answer and use this to calculate a percentages array for each one.
Once we have this grouped value, we can use Object.values
and Array.flatMap
to get the final result.
<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-js -->
const genderResults = [ { Qty: 7, Gender: 'Male', Answer: 'Badminton' }, { Qty: 8, Gender: 'Female', Answer: 'Badminton' }, { Qty: 2, Gender: 'Male', Answer: 'None of the above' }, { Qty: 2, Gender: 'Female', Answer: 'None of the above' }, { Qty: 3, Gender: 'Female', Answer: 'Rugby' }, { Qty: 5, Gender: 'Male', Answer: 'Rugby' }, { Qty: 4, Gender: 'Male', Answer: 'Soccer' }, { Qty: 3, Gender: 'Female', Answer: 'Soccer' }, { Qty: 21, Gender: 'Male', Answer: 'Squash' }, { Qty: 16, Gender: 'Female', Answer: 'Squash' }, { Qty: 1, Gender: 'Other', Answer: 'Squash' }, { Qty: 18, Gender: 'Female', Answer: 'Tennis' }, { Qty: 12, Gender: 'Male', Answer: 'Tennis' } ]
const grouped = genderResults.reduce((acc, row) => {
const key = row.Answer;
acc[key] = acc[key] || { totalQty: 0, entries: [] };
acc[key].entries.push(row);
acc[key].totalQty += row.Qty;
acc[key].percentages = acc[key].entries.map(entry => {
return {
Percentage: Math.round(100 * entry.Qty / acc[key].totalQty),
Gender: entry.Gender,
Answer: entry.Answer
}
})
return acc;
}, {})
const result = Object.values(grouped).flatMap(obj => obj.percentages)
console.log('Result:')
result.forEach(r => console.log(JSON.stringify(r)))
<!-- language: lang-html -->
<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; }
<!-- end snippet -->
答案4
得分: 0
感谢 @nashit-shayan - 修改了您的代码以获得我所需的结果。
const genderPercent = genderResults.map(res => {
return {Gender: res.Gender, Answer: res.Answer, Percentage: Math.round((res.Qty / getCount(res.Answer)) * 100) }
});
function getCount(answer){
return genderResults.reduce((sum, item) => {
if(answer){
if(item.Answer == answer){
sum += item.Qty;
}
}
else{
sum += item.Qty;
}
return sum;
}, 0);
}
console.log(genderPercent);
英文:
Thanks @nashit-shayan - modified your code to get what I was looking for.
const genderPercent = genderResults.map(res =>{
return {Gender: res.Gender, Answer: res.Answer, Percentage: Math.round((res.Qty / getCount(res.Answer)) * 100) }
});
function getCount(answer){
return genderResults.reduce((sum, item)=>{
if(answer){
if(item.Answer == answer){
sum+= item.Qty;
}
}
else{
sum+= item.Qty;
}
return sum
},0)
}
console.log(genderPercent)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论