I have an object where I am trying to structure it by the key names, as I have a Production subject but it has also a 2nd part, each subject has MCQ, CQ, and total which I want in an array of objects but sometimes in the array of object, there will not be MCQ like the English subject.
I am stuck on adding the subject parts with value in the array of objects.
const result = {
id: 427,
merit: "1",
name: "Israt Jahan",
group_name: "business",
GPA: "4.4",
english_1st_CQ: "24",
english_1st_total: "24",
english_2nd_CQ: "24",
english_2nd_total: "24",
english_GP: "2",
english_percentage: "48",
english_total: "48",
production_1st_CQ: "32",
production_1st_MCQ: "11",
production_1st_total: "43",
production_2nd_CQ: "26",
production_2nd_MCQ: "11",
production_2nd_total: "37",
production_GP: "5",
production_percentage: "80",
roll: 1401,
total_failed: "0",
total_gp: "24",
total_marks: "439",
};
const subjectAdd = (arr, title) => {
const node = arr.find((node) => node.subject === title) ?? {
subject: title.split(" ")[0],
part_1: [],
part_2: [],
};
if (arr.every((e) => e.subject !== node.subject)) arr.push(node);
return node;
};
const subjects = (data) => {
const subject = [];
Object.entries(data).map(([key, value]) => {
const [subjectName, subjectPosition, suffix] = key.split("_");
if (
subjectName != "id" &&
subjectName != "date" &&
subjectName != "group" &&
subjectName != "roll" &&
subjectName != "name" &&
subjectName != "merit" &&
subjectName != "GPA" &&
subjectName != "total"
) {
const newSubject = subjectAdd(subject, `${subjectName} ${subjectPosition}`);
const part1 = subjectAdd(newSubject.part_1, `${subjectName} ${subjectPosition}`);
const part2 = subjectAdd(newSubject.part_2, `${subjectName} ${subjectPosition}`);
}
});
return subject;
};
console.log("result", subjects(result));
Expected output:
[
{
subject: "english",
part_1: [
{
subject: "english 1st",
CQ: 48,
total: 48,
},
],
part_2: [
{
subject: "english 2nd",
CQ: 48,
total: 48,
},
],
},
{
subject: "production",
part_1: [
{
subject: "production 1st",
MCQ:12,
CQ: 48,
total: 48,
},
],
part_2: [
{
subject: "production 2nd",
MCQ:15,
CQ: 48,
total: 48,
},
],
},
];
CodePudding user response:
This approach checks for 'english' or 'production' and uses that to decide how to handle the data. If more subjects are added, an array could be used.
I left all the code in a single function for simplicity.
const result = {
id: 427,
merit: "1",
name: "Israt Jahan",
group_name: "business",
GPA: "4.4",
english_1st_CQ: "24",
english_1st_total: "24",
english_2nd_CQ: "24",
english_2nd_total: "24",
english_GP: "2",
english_percentage: "48",
english_total: "48",
production_1st_CQ: "32",
production_1st_MCQ: "11",
production_1st_total: "43",
production_2nd_CQ: "26",
production_2nd_MCQ: "11",
production_2nd_total: "37",
production_GP: "5",
production_percentage: "80",
roll: 1401,
total_failed: "0",
total_gp: "24",
total_marks: "439",
};
const subjects = (data) => {
const subject = [];
Object.entries(data).map(([key, value]) => {
const [subjectName, subjectPosition, suffix] = key.split("_");
if (
subjectName === "english" || subjectName === "production"
) {
let subj = subject.find(element => element.subject === subjectName);
if (subj === undefined) {
// initialize the subject
subject.push(subj = {
subject: subjectName
});
}
// check if the first character is a digit
if (/^\d/.test(subjectPosition)) {
let pos = "part_" subjectPosition[0];
if (typeof subj[pos] === "undefined") {
// initializae the position
subj[pos] = { subject: subjectName " " subjectPosition };
}
subj[pos][suffix] = value;
}
}
});
return subject;
};
console.log("result", subjects(result));
CodePudding user response:
I'd try to not overcomplicate trying to make everything work with a single function subjectAdd
, but rather having two separate functions - subjectAdd
and partAdd
, for example:
const result = {
id: 427,
merit: "1",
name: "Israt Jahan",
group_name: "business",
GPA: "4.4",
english_1st_CQ: "24",
english_1st_total: "24",
english_2nd_CQ: "24",
english_2nd_total: "24",
english_GP: "2",
english_percentage: "48",
english_total: "48",
production_1st_CQ: "32",
production_1st_MCQ: "11",
production_1st_total: "43",
production_2nd_CQ: "26",
production_2nd_MCQ: "11",
production_2nd_total: "37",
production_GP: "5",
production_percentage: "80",
roll: 1401,
total_failed: "0",
total_gp: "24",
total_marks: "439",
};
const subjectAdd = (arr, title) => {
const node = arr.find((node) => node.subject === title) ?? {
subject: title,
part_1: [],
part_2: [],
};
if (arr.every((e) => e.subject !== node.subject)) arr.push(node);
return node;
};
const partAdd = (arr, title, key, value) => {
const node = arr.find((node) => node.subject === title) ?? {
subject: title,
};
node[key] = value;
if (arr.every((e) => e.subject !== node.subject)) arr.push(node);
return node;
};
const subjects = (data) => {
const subject = [];
Object.entries(data).map(([key, value]) => {
const [subjectName, subjectPosition, suffix] = key.split("_");
if (
subjectName != "id" &&
subjectName != "date" &&
subjectName != "group" &&
subjectName != "roll" &&
subjectName != "name" &&
subjectName != "merit" &&
subjectName != "GPA" &&
subjectName != "total"
) {
const newSubject = subjectAdd(subject, `${subjectName}`);
if (subjectPosition === '1st') {
partAdd(newSubject.part_1, `${subjectName} ${subjectPosition}`, suffix, value);
} else if (subjectPosition === '2nd') {
partAdd(newSubject.part_2, `${subjectName} ${subjectPosition}`, suffix, value);
}
}
});
return subject;
};
console.log("result", subjects(result));