Last active
January 23, 2022 22:35
-
-
Save ZarishIqbal/41312da821366dc2dbbdbbdf6bc8f393 to your computer and use it in GitHub Desktop.
This file contains 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
const list = [ | |
{ | |
"title": "Getting started", | |
"reset_lesson_position": false, | |
"lessons": [ | |
{"name": "Welcome"}, | |
{"name": "Installation"} | |
] | |
}, | |
{ | |
"title": "Basic operator", | |
"reset_lesson_position": false, | |
"lessons": [ | |
{"name": "Addition / Subtraction"}, | |
{"name": "Multiplication / Division"} | |
] | |
}, | |
{ | |
"title": "Advanced topics", | |
"reset_lesson_position": true, | |
"lessons": [ | |
{"name": "Mutability"}, | |
{"name": "Immutability"} | |
] | |
} | |
] | |
list.reduce( | |
(agg, curr, index) => { | |
let position = curr.reset_lesson_position ? 1 : agg.index; | |
return { | |
list: [ | |
...agg.list, | |
{ | |
...curr, | |
position: index + 1, | |
lessons: curr.lessons.map((lesson) => { | |
return { ...lesson, position: position++ }; | |
}), | |
}, | |
], | |
index: position, | |
}; | |
}, | |
{ list: [], index: 1 } | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment