I'm working on an e-commerce application where users can add stand-alone products like speaker or they can add a bundle product like desktop that has options like mouse, keyboard etc. All the products and options are finally added as cart items to the cart.
For example
Desktop
- Mouse
- Keyboard
Speaker
After adding above the cart will have items like below,
Shopping Cart
Line item 1 - Desktop - Parent-Line NULL
Line Item 2 - Mouse - Parent-Line Item 1
Line Item 3 - Keyboard - Parent-Line Item 1
Line Item 4 - Speaker - Parent-Line NULL
If I remove the product desktop from the cart all of it's options should also be removed. At the same time we want to access all the line items as an array too.
For example,
for (var items in shoppingCart.items) {
console.log(item);
}
// "Desktop", "Mouse", "Keyboard", "Speaker"
If I use an array to store the items then I need to work on the logic to remove the child options on removing parent. I couldn't come-up with an excellent data structure to solve this problem. Any ideas would be very helpful.