Imagine a scenario where you have a complex array of objects structured like this:
[
{
"title": "Fundamentals",
"order": 1,
"lessonRef": "fundamentals",
"children": [
{
"title": "History",
"order": 1,
"lessonRef": "history",
"children": []
},
{
"title": "NEW NODE 1",
"lessonRef": "NEW NODE 1 STUB",
"children": []
},
{
"title": "Math",
"order": 2,
"lessonRef": "math",
"children": []
},
{
"title": "NEW NODE 2",
"lessonRef": "NEW NODE 2 STUB",
"children": []
}
{
"title": "Geography",
"order": 3,
"lessonRef": "geography",
"children": []
}
]
},
{
"title": "Basics",
"order": 2,
"lessonRef": "basics",
"children": []
}
]
How can you:
- Sequentially go through each node,
- Detect any
new node
that does not have theorder
field and assign it the next number depending on its position in the array, - Subsequently, increment the order of every existing sibling after it while also
- Considering any other newly added nodes that come after it?
I am seeking a lodash method to help me initiate the process before delving into pure javascript.
UPDATE: I have shared my solution -- note that the order of elements in the array is set, but the question will be expanded to address scenarios where the order is not predefined.