我正在努力訂購JavaScript對象。這是我必須分類的部分數據。排序和重構json對象w /下劃線
我想按ParentMemberCode
對每個對象進行分組,然後按Order
對它們進行排序。
{
"MemberCode": "B1G",
"ParentMemberCode": "B1_GA",
"MemberName": "Gross value added at basic prices, total activity",
"Order": 1
},
{
"MemberCode": "P119",
"ParentMemberCode": "B1_GA",
"MemberName": "Financial Intermediation Services Indirectly Measured (FISIM)",
"Order": 2
},
{
"MemberCode": "B1G_P119",
"ParentMemberCode": "B1_GA",
"MemberName": "Gross value added at basic prices, excluding FISIM",
"Order": 3
},
{
"MemberCode": "D21_D31",
"ParentMemberCode": "B1_GA",
"MemberName": "Taxes less subsidies on products",
"Order": 4
},
{
"MemberCode": "B1_GE",
"ParentMemberCode": "GDP",
"MemberName": "Gross domestic product - expenditure approach",
"Order": 1
},
{
"MemberCode": "GDP",
"ParentMemberCode": "GDP",
"MemberName": "Gross domestic product",
"Order": 1
},
{
"MemberCode": "B1_GI",
"ParentMemberCode": "GDP",
"MemberName": "Gross domestic product - income approach",
"Order": 2
},
{
"MemberCode": "B1_GA",
"ParentMemberCode": "GDP",
"MemberName": "Gross domestic product at market prices - output approach",
"Order": 3
}
我想重組和排序的數據是這樣的:
{
"MemberCode": "GDP",
"ParentMemberCode": "GDP",
"MemberName": "Gross domestic product",
"Order": 1
}
{
"MemberCode": "B1_GE",
"ParentMemberCode": "GDP",
"MemberName": "Gross domestic product - expenditure approach",
"Order": 1
},
,
{
"MemberCode": "B1_GI",
"ParentMemberCode": "GDP",
"MemberName": "Gross domestic product - income approach",
"Order": 2
},
{
"MemberCode": "B1_GA",
"ParentMemberCode": "GDP",
"MemberName": "Gross domestic product at market prices - output approach",
"Order": 3,
[
{
"MemberCode": "B1G",
"ParentMemberCode": "B1_GA",
"MemberName": "Gross value added at basic prices, total activity",
"Order": 1
},
{
"MemberCode": "P119",
"ParentMemberCode": "B1_GA",
"MemberName": "Financial Intermediation Services Indirectly Measured (FISIM)",
"Order": 2
},
{
"MemberCode": "B1G_P119",
"ParentMemberCode": "B1_GA",
"MemberName": "Gross value added at basic prices, excluding FISIM",
"Order": 3
},
{
"MemberCode": "D21_D31",
"ParentMemberCode": "B1_GA",
"MemberName": "Taxes less subsidies on products",
"Order": 4
},
]
}
它真的複雜到我,所以我需要你的幫助。 你有什麼想法嗎?
請不要限制你的想法w/underscore.js。
@JustAnil謝謝你明確我的想法。 – user2495856