我有一个巨大的嵌套对象数组,看起来像这样(添加了76个产品,并描绘了一幅好照片):
[
{
"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",
"ShipVia": 3,
"Freight": 32.38,
"ShipName": "Vins et alcools Chevalier",
"ShipAddress": "59 rue de l'Abbaye",
"ShipCity": "Reims",
"ShipRegion": null,
"ShipPostalCode": "51100",
"ShipCountry": "France",
"order_details": {
"OrderID": 10248,
"ProductID": 11,
"UnitPrice": 14,
"Quantity": 12,
"Discount": 0
}
},
{
"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",
"ShipVia": 1,
"Freight": 0.12,
"ShipName": "LILA-Supermercado",
"ShipAddress": "Carrera 52 con Ave. Bolívar #65-98 Llano Largo",
"ShipCity": "Barquisimeto",
"ShipRegion": "Lara",
"ShipPostalCode": "3508",
"ShipCountry": "Venezuela",
"order_details": {
"OrderID": 10296,
"ProductID": 11,
"UnitPrice": 16.8,
"Quantity": 12,
"Discount": 0
}
} ... etc.
我需要遍历Orders和Order_Details并将每个Product的订单合并到一个数组中。产品有多个订单,每个订单都有一个订单详细信息,所以我的理想输出看起来像这样:
{
"ProductID": 11,
"ProductName": "Queso Cabrales",
"SupplierID": 5,
"CategoryID": 4,
"QuantityPerUnit": "1 kg pkg.",
"UnitPrice": 21,
"UnitsInStock": 22,
"UnitsOnOrder": 30,
"ReorderLevel": 30,
"Discontinued": false,
"OrderID": [10248, 10296]
"CustomerID":[ "VINET", "LILAS"]
"EmployeeID":[ 5, 6]
"OrderDate":[ "1996-07-04T05:[0:[0.000Z", "1996-09-03T05:00:00.000Z"]
"RequiredDate":[ "1996-08-01T05:[0:[0.000Z", "1996-10-01T05:00:00.000Z"]
"ShippedDate":[ "1996-07-16T05:[0:[0.000Z", "1996-09-11T05:00:00.000Z"]
"ShipVia":[ 3, 1]
"Freight":[ 32.38, 0.12]
"ShipName":[ "Vins et alcools Chevalier", "LILA-Supermercado"]
"ShipAddress":[ "59 rue de l'Abbaye", "Carrera 52 con Ave. Bolívar #65-98 Llano Largo"]
"ShipCity":[ "Reims", "Barquisimeto"]
"ShipRegion":[ null, "Lara"]
"ShipPostalCode":[ "51100", "3508"]
"ShipCountry":[ "France", "Venezuela"]
"OrderID":[ 10248, 10296]
"ProductID":[ 11, 11]
"UnitPrice":[ 14, 16.8]
"Quantity":[ 12, 12]
"Discount":[ 0, 0]
}
我最大的问题是:
我希望将其发送到客户端以使用D3进行可视化。如果您可以使用如下数据输入对堆积区域图进行成像:
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]
]}
}
我使用_.merge和_.assign失败了 - 任何对正确方向的支持或推动都会受到赞赏。
答案 0 :(得分:0)
我希望我做得对,所以我建议制作一些单独的对象,例如产品和订单,其中对象的每个键都是元素,这样你的订单对象就是这样的:
var Orders = {
11:{"ProductID":11,"ProductName":...},
12:{"ProductID":12,"ProductName":...},
}
因此,您可以通过其ID轻松获取订单参数。
您可以对 order_details 执行相同操作,但我认为没有任何理由不将其属性包含在订单对象中。