2

JavaScript オブジェクトの順序付けに苦労しています。これが私がソートしなければならないデータの部分です。

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
        },
    ]
}

私にはとても複雑なので、あなたの助けが必要です。それについて何か考えはありますか?

underscore.js でアイデアを制限しないでください。

4

2 に答える 2

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、各オブジェクトを参照する別の辞書を作成して、各検索がMemberCode平均ログ(アイテムの数)を取得し、子アイテムを親のChildren配列に追加します(配列を追加するときはあなた次第です)。その後、おそらく検索中またはその後、持っているすべてのオブジェクトを除外しますすでに親が割り当てられています (それは自分自身ではありません)。

于 2013-07-10T15:34:15.093 に答える