我正在构建一个JSON对象,供DataTables子行选项使用,以显示各种客户的车道费率。我已经将这些数据收集到JSON对象中。
我一直在弄混这个js小提琴(http://jsfiddle.net/7v2Cs/1/)来尝试合并对象,但是由于它们使用不同的格式,因此并没有取得太大的成功。 (基于以下问题:Combining Nested Objects in a JSON Array)
数据的格式如下:
var cData =
[
{
"customer_name":"Company1",
"type_of_shipment":"F",
"pickup_city":"MARTINSBURG",
"pickup_state":"WV",
"consignee_city":"BALL GROUND",
"consignee_state":"GA",
"AVG( proj_revenue )":"1683.333333",
"AVG( proj_gross_profit )":"216.666667",
"COUNT( pro_num )":3
},
{
"customer_name":"Company2",
"type_of_shipment":"V",
"pickup_city":"ALLENTOWN",
"pickup_state":"PA",
"consignee_city":"ROBBINSVILLE",
"consignee_state":"NJ",
"AVG( proj_revenue )":"572.320000",
"AVG( proj_gross_profit )":"222.320000",
"COUNT( pro_num )":2
},
{
"customer_name":"Company2",
"type_of_shipment":"V",
"pickup_city":"BUENA PARK",
"pickup_state":"CA",
"consignee_city":"NEWARK",
"consignee_state":"CA",
"AVG( proj_revenue )":"1132.820000",
"AVG( proj_gross_profit )":"-217.180000",
"COUNT( pro_num )":1
},
{
"customer_name":"Company3",
"type_of_shipment":"V",
"pickup_city":"BENSENVILLE",
"pickup_state":"IL",
"consignee_city":"STAUNTON",
"consignee_state":"VA",
"AVG( proj_revenue )":"3725.000000",
"AVG( proj_gross_profit )":"1025.000000",
"COUNT( pro_num )":2
}
]
var cData2 = [
{
"customer":"Company1",
"shipments1":1,
"profit1":"0.00",
"gp_percent1":"0.0000",
"agp1":"0.00"
},
{
"customer":"Company2",
"shipments1":0,
"profit1":"0.00",
"gp_percent1":"0.0000",
"agp1":"0.00"
},
{
"customer":"Company3",
"shipments1":0,
"profit1":"0.00",
"gp_percent1":"0.0000",
"agp1":"0.00"
}
]
想法是将数据格式化为:
var cDataFinal =
[
{
"customer":"Company1",
"shipments1":1,
"profit1":"0.00",
"gp_percent1":"0.0000",
"agp1":"0.00",
"lanes":[
{
"customer_name":"Company1",
"type_of_shipment":"F",
"pickup_city":"MARTINSBURG",
"pickup_state":"WV",
"consignee_city":"BALL GROUND",
"consignee_state":"GA",
"AVG( proj_revenue )":"1683.333333",
"AVG( proj_gross_profit )":"216.666667",
"COUNT( pro_num )":3
}]
},
{
"customer":"Company2",
"shipments1":0,
"profit1":"0.00",
"gp_percent1":"0.0000",
"agp1":"0.00",
"lanes":[
{
"customer_name":"Company2",
"type_of_shipment":"V",
"pickup_city":"ALLENTOWN",
"pickup_state":"PA",
"consignee_city":"ROBBINSVILLE",
"consignee_state":"NJ",
"AVG( proj_revenue )":"572.320000",
"AVG( proj_gross_profit )":"222.320000",
"COUNT( pro_num )":2
},
{
"customer_name":"Company2",
"type_of_shipment":"V",
"pickup_city":"BUENA PARK",
"pickup_state":"CA",
"consignee_city":"NEWARK",
"consignee_state":"CA",
"AVG( proj_revenue )":"1132.820000",
"AVG( proj_gross_profit )":"-217.180000",
"COUNT( pro_num )":1
}]
},
{
"customer":"Company3",
"shipments1":0,
"profit1":"0.00",
"gp_percent1":"0.0000",
"agp1":"0.00",
"lanes":[
{
"customer_name":"Company3",
"type_of_shipment":"V",
"pickup_city":"BENSENVILLE",
"pickup_state":"IL",
"consignee_city":"STAUNTON",
"consignee_state":"VA",
"AVG( proj_revenue )":"3725.000000",
"AVG( proj_gross_profit )":"1025.000000",
"COUNT( pro_num )":2
}]
}
]
是否有任何建议或可用的工具来合并两个字段相似的JSON对象,并将一组数据作为子集插入?
答案 0 :(得分:0)
您可能不需要考虑合并。您只需要遍历一个数组并添加一个属性即可。
您可以尝试这样的事情:
public String goAdd(Model model) {
model.addAttribute(new DtoCustomer());
return "customer/edit";
}
答案 1 :(得分:0)
您可以简单地根据customer_name过滤cData并将其添加到cData2中的lanes属性。
var mergedData = cData2.map(data2 => {
const filterData = cData.filter(cData => { return cData.customer_name === data2.customer })
data2.lanes = filterData;
return data2;
});
希望这会有所帮助