I'm facing a conundrum where I have to loop over an array of nested objects (object of objects) that could have other objects and arrays. That is, the array has objects that could have other objects with arrays of objects and so on.
The data structure is for a list (like a to-do list) that could have another list and that other list could have another list and so on:
[
"Buy milk",
{ //<----------------- item 2 is is an object that represents sub-items
itemName: "Buy Meat", //<------------ name of item 2 on list
subList: [ //<------ sub-items with an item of items
"Beef",
{
itemName: "Fish",
subList: ["Tilapia", "Catfish", "Monkfish", "Halibut"]
} ,
"Chicken",
],
},
"Buy cooking oil",
"Buy baking soda",
{ //<------------------- item 6 is an object that represents sub-items (sub-todo)
itemName: "Buy Vegetable", //<------------ name of item 6 on list
itemList: ["Cabbage", "Carrot", "Tomatoe", "Lettuce"] //<------ sub-items
},
"Buy bread"
]
How do I loop over a data structure like this? Also, is there a better way to represent such a list?