My object which maps student id with marks is as follows:
[
{id: 111, marks: [{sub: 'eng', mark: 90}, {sub: 'maths', mark: 20}]},
{id: 222},
{id: 333, marks: []},
{id: 444, marks: [{sub: 'eng', mark: 70}]}
]
I would like to reduce it as follows:
{
marks[0]: "0:eng:90", // studentIndex:subject_name:mark
marks[1]: "0:maths:20",
marks[2]: "3:eng:70"
}
In the above result, the key is "marks[]" and the value is a string which is a concatenation of studentIndex, the subject and the mark. So here 0:eng:90 means that student with index of 0 has obtained 90 marks in english subject
I am using lodash and I have tried the following:
reduce(studentList, (acc, student, studentIndex) => {
map(get(student, 'marks'), (marks) => {
acc[`marks[${keys(acc).length}]`] = `${studentIndex}:${marks.sub}:${marks.mark}`;
});
return acc;
}, {});
Is there any other better way to do this?