A Pen by RAGHAV PRASAD on CodePen.
Created
December 26, 2022 14:40
-
-
Save raghavprasad31/97b8e0f8f1287b316c8c4a1a4d50a6a7 to your computer and use it in GitHub Desktop.
Array HOC Problem-8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let prateekMarksData = { | |
| name: "Prateek", | |
| subject1: "Javascript", | |
| subject2: "HTML", | |
| subject3: "CSS", | |
| subject4: null, | |
| subject5: null, | |
| marks1: 90, | |
| marks2: 81, | |
| marks3: 80, | |
| marks4: null, | |
| marks5: null, | |
| } | |
| let nrupulMarksData = { | |
| name: "Nrupul", | |
| subject1: "Java", | |
| subject2: "Pyton", | |
| subject3: null, | |
| subject4: null, | |
| subject5: null, | |
| marks1: 95, | |
| marks2: 85, | |
| marks3: null, | |
| marks4: null, | |
| marks5: null, | |
| } | |
| let allStudentsMarksData = [prateekMarksData, nrupulMarksData]; | |
| let massagedData; | |
| massagedData=allStudentsMarksData.reduce((acc, item)=>{ | |
| let obj={}; | |
| obj.name=item.name; | |
| obj.marks=[]; | |
| item.subject1 && obj.marks.push({subject: item.subject1, marks: item.marks1 }); | |
| item.subject2 &&obj.marks.push({subject: item.subject2, marks: item.marks2 }); | |
| item.subject3 && obj.marks.push({subject: item.subject3, marks: item.marks3 }); | |
| item.subject4 &&obj.marks.push({subject: item.subject4, marks: item.marks4 }); | |
| item.subject5 &&obj.marks.push({subject: item.subject5, marks: item.marks5 }); | |
| acc.push(obj); | |
| return acc; | |
| }, []); | |
| console.log(massagedData); | |
| /* | |
| ------------------------------------------------ | |
| massage the `allStudentsMarksData` to get a new | |
| array called `massagedData` | |
| Instead of separate enties for 5 subjects and their marks, | |
| in the new array, we just have one entry called marks. | |
| marks is an array of objects. the objects of marks | |
| contains subject and score. | |
| ------------------------------------------------ | |
| Expected output from `massagedData` | |
| [ | |
| { | |
| name: "Prateek", | |
| marks: [ | |
| {subject: 'Javascript', score: 90}, | |
| {subject: 'HTML', score: 81}, | |
| {subject: 'CSS', score: 80} | |
| ] | |
| }, | |
| { | |
| name: "Nrupul", | |
| marks: [ | |
| {subject: 'Java', score: 95}, | |
| {subject: 'Python', score: 85} | |
| ] | |
| }, | |
| ] | |
| */ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment