1

次のjson配列があります。

{
"id": "1004482725",
"parent": 0,
"industry": "Akrotiri",
"industry_id": "root",
"name": "Comp",
"percentage": "0",
"report": "null",
"description": "",
"subsidiary": "3",
"children": [{
    "id": "1004482733",
    "parent": "1004482725",
    "industry": "Akrotiri",
    "industry_id": "industry9",
    "name": "Comp11",
    "percentage": "30",
    "report": "null",
    "description": "",
    "subsidiary": "1",
    "children": [{
        "id": "1004482734",
        "parent": "1004482733",
        "industry": "Akrotiri",
        "industry_id": "industry10",
        "name": "Comp110",
        "percentage": "30.00",
        "report": "null",
        "description": "",
        "subsidiary": "1",
        "children": [{
            "id": "aa02634",
            "parent": "1004482734",
            "industry": "",
            "industry_id": "0",
            "name": "Sef 4",
            "percentage": "4",
            "report": "null",
            "description": "",
            "subsidiary": "0",
            "children": []
        }]
    }, {
        "id": "1004482735",
        "parent": "1004482733",
        "industry": "Akrotiri",
        "industry_id": "industry11",
        "name": "Comp111",
        "percentage": "30.00",
        "report": "null",
        "description": "",
        "subsidiary": "1",
        "children": [{
            "id": "aa02635",
            "parent": "1004482735",
            "industry": "",
            "industry_id": "0",
            "name": "Sef 5",
            "percentage": "5",
            "report": "null",
            "description": "",
            "subsidiary": "0",
            "children": []
        }]
    }, {
        "id": "1004482736",
        "parent": "1004482733",
        "industry": "Akrotiri",
        "industry_id": "industry12",
        "name": "Comp112",
        "percentage": "30.00",
        "report": "null",
        "description": "",
        "subsidiary": "1",
        "children": [{
            "id": "1004482737",
            "parent": "1004482736",
            "industry": "Akrotiri",
            "industry_id": "industry13",
            "name": "Comp1120",
            "percentage": "100.00",
            "report": "null",
            "description": "",
            "subsidiary": "2",
            "children": [{
                "id": "1004482738",
                "parent": "1004482737",
                "industry": "Akrotiri",
                "industry_id": "industry14",
                "name": "Comp11200",
                "percentage": "100.00",
                "report": "null",
                "description": "",
                "subsidiary": "1",
                "children": [{
                    "id": "1004482739",
                    "parent": "1004482738",
                    "industry": "Akrotiri",
                    "industry_id": "industry15",
                    "name": "Comp112000",
                    "percentage": "100.00",
                    "report": "null",
                    "description": "",
                    "subsidiary": "1",
                    "children": [{
                        "id": "aa02636",
                        "parent": "1004482739",
                        "industry": "",
                        "industry_id": "0",
                        "name": "Sef 6",
                        "percentage": "6",
                        "report": "null",
                        "description": "",
                        "subsidiary": "0",
                        "children": []
                    }]
                }]
            }]
        }, {
            "id": "1004482740",
            "parent": "1004482736",
            "industry": "Akrotiri",
            "industry_id": "industry16",
            "name": "Comp1121",
            "percentage": "100.00",
            "report": "null",
            "description": "",
            "subsidiary": "1",
            "children": [{
                "id": "1004482741",
                "parent": "1004482740",
                "industry": "Akrotiri",
                "industry_id": "industry17",
                "name": "Comp11210",
                "percentage": "100.00",
                "report": "null",
                "description": "",
                "subsidiary": "1",
                "children": [{
                    "id": "aa02637",
                    "parent": "1004482741",
                    "industry": "",
                    "industry_id": "0",
                    "name": "Sef 7",
                    "percentage": "7",
                    "report": "null",
                    "description": "",
                    "subsidiary": "0",
                    "children": []
                }]
            }]
        }, {
            "id": "1004482742",
            "parent": "1004482736",
            "industry": "Akrotiri",
            "industry_id": "industry18",
            "name": "Comp1122",
            "percentage": "30.00",
            "report": "null",
            "description": "",
            "subsidiary": "2",
            "children": [{
                "id": "1004482743",
                "parent": "1004482742",
                "industry": "Akrotiri",
                "industry_id": "industry19",
                "name": "Comp11220",
                "percentage": "100.00",
                "report": "null",
                "description": "",
                "subsidiary": "1",
                "children": [{
                    "id": "aa02638",
                    "parent": "1004482743",
                    "industry": "",
                    "industry_id": "0",
                    "name": "Sef 8",
                    "percentage": "8",
                    "report": "null",
                    "description": "",
                    "subsidiary": "0",
                    "children": []
                }]
            }]
        }]
    }]
}, {
    "id": "1004482726",
    "parent": "1004482725",
    "industry": "Akrotiri",
    "industry_id": "industry2",
    "name": "Comp10",
    "percentage": "30",
    "report": "null",
    "description": "",
    "subsidiary": "1",
    "children": [{
        "id": "1004482732",
        "parent": "1004482726",
        "industry": "Akrotiri",
        "industry_id": "industry8",
        "name": "Comp102",
        "percentage": "30",
        "report": "null",
        "description": "",
        "subsidiary": "1",
        "children": [{
            "id": "aa02633",
            "parent": "1004482732",
            "industry": "",
            "industry_id": "0",
            "name": "Sef 3",
            "percentage": "3",
            "report": "null",
            "description": "",
            "subsidiary": "0",
            "children": []
        }]
    }, {
        "id": "1004482728",
        "parent": "1004482726",
        "industry": "Akrotiri",
        "industry_id": "industry4",
        "name": "Comp101",
        "percentage": "30",
        "report": "null",
        "description": "",
        "subsidiary": "1",
        "children": [{
            "id": "1004482729",
            "parent": "1004482728",
            "industry": "Akrotiri",
            "industry_id": "industry5",
            "name": "Comp1010",
            "percentage": "100.00",
            "report": "null",
            "description": "",
            "subsidiary": "1",
            "children": [{
                "id": "1004482730",
                "parent": "1004482729",
                "industry": "Akrotiri",
                "industry_id": "industry6",
                "name": "Comp10100",
                "percentage": "100.00",
                "report": "null",
                "description": "",
                "subsidiary": "1",
                "children": [{
                    "id": "1004482731",
                    "parent": "1004482730",
                    "industry": "Akrotiri",
                    "industry_id": "industry7",
                    "name": "Comp101000",
                    "percentage": "100.00",
                    "report": "null",
                    "description": "",
                    "subsidiary": "1",
                    "children": [{
                        "id": "aa02632",
                        "parent": "1004482731",
                        "industry": "",
                        "industry_id": "0",
                        "name": "Sef 2",
                        "percentage": "2",
                        "report": "null",
                        "description": "",
                        "subsidiary": "0",
                        "children": []
                    }]
                }]
            }]
        }]
    }, {
        "id": "1004482727",
        "parent": "1004482726",
        "industry": "Akrotiri",
        "industry_id": "industry3",
        "name": "Comp100",
        "percentage": "30",
        "report": "null",
        "description": "",
        "subsidiary": "1",
        "children": [{
            "id": "aa02631",
            "parent": "1004482727",
            "industry": "",
            "industry_id": "0",
            "name": "Sef1",
            "percentage": "1",
            "report": "null",
            "description": "",
            "subsidiary": "0",
            "children": []
        }]
    }]
}, {
    "id": "1004482744",
    "parent": "1004482725",
    "industry": "Akrotiri",
    "industry_id": "industry20",
    "name": "Comp12",
    "percentage": "30",
    "report": "null",
    "description": "",
    "subsidiary": "1",
    "children": [{
        "id": "1004482745",
        "parent": "1004482744",
        "industry": "Akrotiri",
        "industry_id": "industry21",
        "name": "Comp120",
        "percentage": "30.00",
        "report": "null",
        "description": "",
        "subsidiary": "1",
        "children": [{
            "id": "",
            "parent": "",
            "industry": "",
            "industry_id": "",
            "name": "",
            "percentage": "",
            "report": "",
            "description": "",
            "subsidiary": "",
            "children": ""
        }, {
            "id": "1004482748",
            "parent": "1004482745",
            "industry": "Akrotiri",
            "industry_id": "industry24",
            "name": "Comp1210",
            "percentage": "50.00",
            "report": "null",
            "description": "",
            "subsidiary": "2",
            "children": [{
                "id": "1004482749",
                "parent": "1004482748",
                "industry": "Akrotiri",
                "industry_id": "industry25",
                "name": "Comp12100",
                "percentage": "100.00",
                "report": "null",
                "description": "",
                "subsidiary": "1",
                "children": [{
                    "id": "1004482750",
                    "parent": "1004482749",
                    "industry": "Akrotiri",
                    "industry_id": "industry26",
                    "name": "Comp121000",
                    "percentage": "100.00",
                    "report": "null",
                    "description": "",
                    "subsidiary": "1",
                    "children": [{
                        "id": "aa02639",
                        "parent": "1004482750",
                        "industry": "",
                        "industry_id": "0",
                        "name": "Sef 9",
                        "percentage": "9",
                        "report": "null",
                        "description": "",
                        "subsidiary": "0",
                        "children": []
                    }]
                }]
            }]
        }, {
            "id": "",
            "parent": "",
            "industry": "",
            "industry_id": "",
            "name": "",
            "percentage": "",
            "report": "",
            "description": "",
            "subsidiary": "",
            "children": ""
        }]
    }, {
        "id": "1004482746",
        "parent": "1004482744",
        "industry": "Akrotiri",
        "industry_id": "industry22",
        "name": "Comp121",
        "percentage": "30.00",
        "report": "null",
        "description": "",
        "subsidiary": "1",
        "children": [{
            "id": "",
            "parent": "",
            "industry": "",
            "industry_id": "",
            "name": "",
            "percentage": "",
            "report": "",
            "description": "",
            "subsidiary": "",
            "children": ""
        }]
    }, {
        "id": "1004482747",
        "parent": "1004482744",
        "industry": "Akrotiri",
        "industry_id": "industry23",
        "name": "Comp122",
        "percentage": "30.00",
        "report": "null",
        "description": "",
        "subsidiary": "1",
        "children": [{
            "id": "aa02640",
            "parent": "1004482747",
            "industry": "",
            "industry_id": "0",
            "name": "Sef 10",
            "percentage": "10",
            "report": "null",
            "description": "",
            "subsidiary": "0",
            "children": []
        }]
    }]
}]
}

この例では、次のような要素がいくつかあります。

 "children": [{
            "id": "",
            "parent": "",
            "industry": "",
            "industry_id": "",
            "name": "",
            "percentage": "",
            "report": "",
            "description": "",
            "subsidiary": "",
            "children": ""
        }

私が達成したいのは、この「子」を元の配列から削除することです。これまでのところ、この機能を試しましたが、思い通りに動作しません:

 function deleteEmpty(data){
    if (data.children.length)
    {  

        nr = data.children.length;
        for (i=0;i<nr;i++)
        {
           // console.log(data.children[i]);

            if (data.children[i]["id"] == ""){
                console.log(data.children[i]);
                delete data.children[i];
            }                  
                this.deleteEmpty(data.children[i]);

        }
    }
    return data;
}

また、私はここでjsfiddleを作成しました:http://jsfiddle.net/fP7NS/2/ 私を助けてください、または私が間違っていることについていくつかのヒントを教えてください。

4

1 に答える 1

1

最初に (必要に応じて) JSON 文字列をオブジェクトに変換します。プロパティchildrenは、配列の場合もあれば、空の配列の場合もあれば、空の文字列の場合もあります。これにより、ロジックが少し厄介になります。特定の子が空かどうかをテストする関数を作成することをお勧めします。次に、メインループを次のように記述できます。

function isEmpty(node) {
    // logic for testing that a node is empty
}
function deleteEmptyChildren(node) {
    // assumes node is not empty
    if (Array.isArray(node.children)) {
        node.children = node.children.filter(
            function(child) {
                if (isEmpty(child)) {
                    return false;
                } else {
                    deleteEmptyChildren(child);
                    return true;
                }
            }
        );
    }
}
于 2013-03-17T09:47:18.947 に答える