I possess a large array of intricately nested objects, akin to this (imagine adding 76 more products for a clearer picture):
[
{
"ProductID": 11,
"ProductName": "Queso Cabrales",
"SupplierID": 5,
"CategoryID": 4,
"QuantityPerUnit": "1 kg pkg.",
"UnitPrice": 21,
"UnitsInStock": 22,
"UnitsOnOrder": 30,
"ReorderLevel": 30,
"Discontinued": false,
"orders": [
{
"OrderID": 10248,
"CustomerID": "VINET",
"EmployeeID": 5,
"OrderDate": "1996-07-04T05:00:00.000Z",
"RequiredDate": "1996-08-01T05:00:00.000Z",
"ShippedDate": "1996-07-16T05:00:00.000Z",
// More details here
},
{
"OrderID": 10296,
"CustomerID": "LILAS",
"EmployeeID": 6,
"OrderDate": "1996-09-03T05:00:00.000Z",
"RequiredDate": "1996-10-01T05:00:00.000Z",
"ShippedDate": "1996-09-11T05:00:00.000Z",
// More details here
} ... etc.
My task is to iterate through Orders and Order_Details and combine each Product's orders into a single array. Each product has multiple orders with a single order detail, so the desired output should resemble:
{
"ProductID": 11,
"ProductName": "Queso Cabrales",
// Other details merged from orders
}
The major challenges include:
- Accessing order_details nested within each orderID.
- Maintaining the association of one product to multiple orders (which almost broke the internet when I tried to merge everything into one huge array).
I aim to send this data to the client side for visualization using D3. Envision a stacked area chart with input data like:
var graph = {
Spicy-Ramen: {[
[380.00, 12-March-2015], [420.62, 19-March-2015]
]},
Miso-Soup: {[
[230.99, 12-April-2015], [322.60, 22-May-2015]
]},
Vegetable-Shumai: {[
[22.99, 12-June-2015], [121.20, 29-August-2015]
]}
}
I attempted to use _.merge and _.assign without success - any assistance or guidance would be greatly appreciated.