Am trouble in group(in my case I tried reduce and foreach) but while structuring I seem to fail somewhere.
the logic behind the structuring is that the continuousAssement is composed of the (Test, Quiz) and the exam is by its own.
the remark give is by condition from the total give from continousAssement and exam
-> every student is by his own
-> I am using javascript(nodejs)
This is the data(JSON)
{
"studentCode": "22001",
"studentMarkSystemName": "CAT 1",
"studentMark": 10,
"maxMark": 30,
"markType": "Test"
},
{
"studentCode": "1111",
"studentMarkSystemName": "CAT 1",
"studentMark": 20,
"maxMark": 30,
"markType": "Test"
},
{
"studentCode": "1111",
"studentMarkSystemName": "EXAM",
"studentMark": 40,
"maxMark": 50,
"markType": "Exam"
},
{
"studentCode": "22001",
"studentMarkSystemName": "EXAM",
"studentMark": 42,
"maxMark": 50,
"markType": "Exam"
},
{
"studentCode": "22001",
"studentMarkSystemName": "Quiz 1",
"studentMark": 5,
"maxMark": 5,
"markType": "Quiz"
},
{
"studentCode": "1111",
"studentMarkSystemName": "Quiz 1",
"studentMark": 4,
"maxMark": 5,
"markType": "Quiz"
}
I want to transform it to
{
"studentCode": "22001",
"continousAssesment": 15,
"Exam":42,
"maxContinousAssement":35,
"maxExam": 50,
"total":57,
"remark": "pass"
}
{
"studentCode": "1111",
"continousAssesment": 24,
"Exam":40,
"maxContinousAssement":35,
"maxExam": 50,
"total": 64,
"remark": "pass"
}
on my side I have tried this, but doesn't get the actual format as need only grouping by studentCode
var resultArrKeyHolder = [];
var resultArr = [];
data.forEach(function (item) {
resultArrKeyHolder[item.studentCode] =
resultArrKeyHolder[item.studentCode] || {};
var obj = resultArrKeyHolder[item.studentCode];
if (Object.keys(obj).length == 0) resultArr.push(obj);
obj.studentCode = item.studentCode;
obj.marks = obj.marks || [];
obj.marks.push({
studentMark: item.studentMark,
maxMark: item.maxMark,
markType: item.markType,
});
});
appreciate your help!
CodePudding user response:
You can use hash grouping approach:
const data = [{"studentCode": "22001","studentMarkSystemName": "CAT 1","studentMark": 10,"maxMark": 30,"markType": "Test"},{"studentCode": "1111","studentMarkSystemName": "CAT 1","studentMark": 20,"maxMark": 30,"markType": "Test"},{"studentCode": "1111","studentMarkSystemName": "EXAM","studentMark": 40,"maxMark": 50,"markType": "Exam"},{"studentCode": "22001","studentMarkSystemName": "EXAM","studentMark": 42,"maxMark": 50,"markType": "Exam"},{"studentCode": "22001","studentMarkSystemName": "Quiz 1","studentMark": 5,"maxMark": 5,"markType": "Quiz"},{"studentCode": "1111","studentMarkSystemName": "Quiz 1","studentMark": 4,"maxMark": 5,"markType": "Quiz"}];
const emptyCounters = {continousAssesment:0,Exam:0,maxContinousAssement:0,maxExam:0,total:0};
const result = Object.values(data.reduce((acc, {studentCode: code, ...rest}) => {
acc[code] ??= { studentCode: code, ...emptyCounters };
if (rest.markType === "Exam") {
acc[code].Exam = rest.studentMark;
acc[code].maxExam = Math.max(rest.maxMark, acc[code].maxExam)
} else {
acc[code].continousAssesment = rest.studentMark;
acc[code].maxContinousAssement = Math.max(rest.maxMark, acc[code].maxContinousAssement)
}
acc[code].total = rest.studentMark;
acc[code].remark = acc[code].total > 50 ? "pass" : "not pass";
return acc;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0 }