2013-07-10 64 views
2

我正在努力訂購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。

+0

@JustAnil謝謝你明確我的想法。 – user2495856

回答

1

我最近不得不做一些類似的事情,我使用了Underscore.nest(https://github.com/iros/underscore.nest),這是一個整潔的小js插件,您可以在其中指定一個字段來將數據摺疊到中,然後可以使用underscore.sort對生成的樹結構?

+0

它看起來像我在找什麼。我會嘗試這一個。 – user2495856

1

我剛纔說我沒有看到這個大問題在這裏。但後來我意識到你想要的是無效的。

您不能在沒有密鑰的對象中包含項目。

{ 
    "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": "B1_GA", 
    "ParentMemberCode": "GDP", 
    "MemberName": "Gross domestic product at market prices - output approach", 
    "Order": 3, 
    "Children": [ 
     { 
      "MemberCode": "B1G", 
      "ParentMemberCode": "B1_GA", 
      "MemberName": "Gross value added at basic prices, total activity", 
      "Order": 1 
     }, 
.... 

至於創造這個,你只需要能夠通過你的對象最上面的數組中遍歷,查找每個ParentMemberCode通過它的MemberCode(您可能要創建另一個字典被引用的每個對象直到迭代完成之前,以便每次查找都需要平均日誌(項目數)),並將子項添加到父項的Children數組(當您添加數組完全取決於您時),然後可能與您一樣搜索或之後,過濾掉所有已經分配了父項的對象(不是它們自己)。

+0

感謝您評論我的錯誤。我應該像你一樣命名數組的名稱。 – user2495856