以递归方式从JSON数组中删除元素

时间:2013-03-17 09:32:59

标签: javascript jquery json

我有以下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/ 请帮帮我,或者给我一些关于我做错的提示。

1 个答案:

答案 0 :(得分: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;
                }
            }
        );
    }
}