A Pen by RAGHAV PRASAD on CodePen.
Created
December 26, 2022 10:19
-
-
Save raghavprasad31/735f6ea0eee627eadeee0b2e84c3544e to your computer and use it in GitHub Desktop.
Array HOC Problem-7
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 subjectsData = [ | |
| {id: 1, name: 'Javascript'}, | |
| {id: 2, name: 'HTML'}, | |
| {id: 3, name: 'CSS'}, | |
| {id: 4, name: 'Java'}, | |
| {id: 5, name: 'Rust'}, | |
| ] | |
| // code your key-value object for subjects here | |
| let subjects={}; | |
| for(let i=0; i<subjectsData.length; i++){ | |
| subjects[subjectsData[i].id]=subjectsData[i].name; | |
| //subjects[subjectsData[i].id]=subjectsData[i].name; | |
| } | |
| //console.log(subjects); | |
| let students = [ | |
| {id: 1, name: 'Prateek', subjectID: 5}, | |
| {id: 2, name: 'Yogesh', subjectID: 2}, | |
| {id: 3, name: 'Nrupul', subjectID: 4}, | |
| {id: 4, name: 'Prateek', subjectID: 1}, | |
| ] | |
| let newObj =students.reduce((acc, item)=>{ | |
| acc[item.name]=acc[item.name]||[]; | |
| acc[item.name].push(subjects[item.subjectID]); | |
| return acc;}, {}) /* your array method here */ | |
| console.log(newObj); | |
| /* | |
| ---------------------------------- | |
| create a new object called `newObj` using the `students` array & | |
| the `subjectsData` array. | |
| Hint: consider creating an extra key-value object for quickly accessing subject names | |
| ---------------------------------- | |
| Expected Output of `newObj`: | |
| { | |
| Prateek: ["Rust", "Javascript"], | |
| Yogesh: ["HTML"], | |
| Nrupul: ["Java"], | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment