英文:
How can I achieve a similar result to a SQL SUM and GROUP BY query in JavaScript and ReactJS for an array of objects?
问题
I recommend using the Array.reduce
method along with an object to accumulate the sums for different ProjectTypes. Here's a modified version of your code:
const frames = [
// Your array of objects here
];
const sumByProjectType = (array) => {
const result = {};
array.forEach((item) => {
const { ProjectType, Amount, Hours } = item;
if (result[ProjectType]) {
result[ProjectType].TotalAmount += Amount;
result[ProjectType].TotalHours += Hours;
} else {
result[ProjectType] = {
ProjectType,
TotalAmount: Amount,
TotalHours: Hours,
};
}
});
return Object.values(result);
};
const result = sumByProjectType(frames);
console.log(result);
This code will create an object where keys are ProjectTypes, and values are objects with the summed TotalAmount and TotalHours. Finally, Object.values(result)
will give you an array of the desired result.
英文:
Filtering and Summing Specific Object Values in Array
I have an interesting problem to solve. I am working on a ReactJS component. I have an array of objects as follows:
const frames = [
{
ProjectName: "Blue",
EmployeeGroup: false,
Amount: 50,
Hours: 15,
ProjectType: "Romeo",
},
{
ProjectName: "Red",
EmployeeGroup: false,
Amount: 20,
Hours: 5,
ProjectType: "Lima",
},
{
ProjectName: "Green",
EmployeeGroup: true,
Amount: 40,
Hours: 10,
ProjectType: "Lima",
}
]
The idea is to sum the Amount and Hours for the different ProjectType values and ignore the EmployeeGroup value as it is irrelevant. So the end result would ideally be an object that has the following key value pairs:
{
ProjectType: "Lima",
TotalAmount: 60,
TotalHours: 15,
},
{
ProjectType: "Romeo",
TotalAmount: 50,
TotalHours: 15,
}
In SQL this is fairly straightforward, as one could simply do a SUM(TotalAmount) and SUM(TotalHours) and GROUP BY ProjectType. I want to achieve something similar in JavaScript but have been unsuccessful thus far.
I have tried the array.filter and array.reduce methods to try to achieve the desired result. My code looks like this:
const filterByProjectType = (array, projectType) => {
const reducer = (array) => {
const reduced = array.reduce(
(accumulator, currentVal) => accumulator + currentVal.Amount, 0
);
return reduced;
}
const filtered = reduced(array.filter((item) => item.ProjectType.includes(projectType)));
return filtered
}
console.log(filterByProjectType(frames, "Lima"))
This currently gives me the value of 60 which is the desired and correct value of TotalAmount of the ProjectType Lima. I know I could repeat this process and then compile the results into an object, but as usual, I believe a quicker way exists, maybe using the array.group() or something similar. What do you recommend to achieve the desired result?
答案1
得分: 1
一种方法是首先创建最终对象...
const items = {};
frames.forEach(({ ProjectType, Amount, Hours }) => {
if (!items[ProjectType]) {
items[ProjectType] = { ProjectType, TotalAmount: 0, TotalHours: 0 };
}
items[ProjectType].TotalAmount += Amount;
items[ProjectType].TotalHours += Hours;
});
const output = Object.values(items);
console.log(output);
英文:
One way would be to create the final object first...
const items = {};
frames.forEach( ({ProjectType, Amount, Hours}) => {
if(!items[ProjectType]){
items[ProjectType] = {ProjectType, TotalAmount: 0, TotalHours: 0};
}
items[ProjectType].TotalAmount += Amount;
items[ProjectType].TotalHours += Hours;
});
const output = Object.values(items);
console.log(output);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论