Skip to content

Instantly share code, notes, and snippets.

@ZarishIqbal
Last active January 23, 2022 22:35
Show Gist options
  • Save ZarishIqbal/41312da821366dc2dbbdbbdf6bc8f393 to your computer and use it in GitHub Desktop.
Save ZarishIqbal/41312da821366dc2dbbdbbdf6bc8f393 to your computer and use it in GitHub Desktop.
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