0

我有一个 JSON 字符串:

var data  = {"categories":
                        [
                             {"id":1,"parent":0,"name":"Category A","description":"Category description","products":"11","subcategories":[]},
                             {"id":2,"parent":0,"name":"Category B","description":"Category description","products":"11","subcategories":
                                    [
                                        {"id":6,"parent":2,"name":"Subcategory F","description":"Category description", "products":"2","subcategories":[]},
                                        {"id":7,"parent":2,"name":"Subcategory G","description":"Category description","products":"7","subcategories":[]}
                                    ]
                             },
                             {"id":3,"parent":0,"name":"Category C","description":"Category description","products":"4","subcategories":
                                    [
                                        {"id":8,"parent":3,"name":"Subcategory H","description":"Category description","products":"8","subcategories":[]}
                                    ]
                             },
                             {"id":4,"parent":0,"name":"Category D","description":"Category description","products":"45","subcategories":
                                    [
                                        {"id":9,"parent":4,"name":"Subcategory I","description":"Category description","products":"2","subcategories":
                                            [
                                                {"id":10,"parent":9,"name":"Subcategory J","description":"Category description","products":"54","subcategories":[]}
                                            ]
                                        }
                                    ]
                            },{"id":5,"parent":0,"name":"Category E","description":"Category description","products":"89","subcategories":[]}
                        ]
                };

如何通过 id 访问数据?例如我需要得到这个 id = 10 的 sub_object:

var requested = request(data, 10);

function request (data, id) {

var output = {};

...code

output = {"id":10,"parent":9,"name":"Subcategory J","description":"Category description","products":"54","subcategories":[]}

return output;

}
4

1 回答 1

0

基本上你可以通过一个简单的递归来做到这一点:http: //jsfiddle.net/99UYU/

var data  = {"categories":
                        [
                             {"id":1,"parent":0,"name":"Category A","description":"Category description","products":"11","subcategories":[]},
                             {"id":2,"parent":0,"name":"Category B","description":"Category description","products":"11","subcategories":
                                    [
                                        {"id":6,"parent":2,"name":"Subcategory F","description":"Category description", "products":"2","subcategories":[]},
                                        {"id":7,"parent":2,"name":"Subcategory G","description":"Category description","products":"7","subcategories":[]}
                                    ]
                             },
                             {"id":3,"parent":0,"name":"Category C","description":"Category description","products":"4","subcategories":
                                    [
                                        {"id":8,"parent":3,"name":"Subcategory H","description":"Category description","products":"8","subcategories":[]}
                                    ]
                             },
                             {"id":4,"parent":0,"name":"Category D","description":"Category description","products":"45","subcategories":
                                    [
                                        {"id":9,"parent":4,"name":"Subcategory I","description":"Category description","products":"2","subcategories":
                                            [
                                                {"id":10,"parent":9,"name":"Subcategory J","description":"Category description","products":"54","subcategories":[]}
                                            ]
                                        }
                                    ]
                            },{"id":5,"parent":0,"name":"Category E","description":"Category description","products":"89","subcategories":[]}
                        ]
                };

function recurseArr(arr,id){

    for(var i=0;i<arr.length;i++){
        var item = arr[i];   
        if(item.id == id){
             return item;   
        }
        var subcategories = item.subcategories;
        var ret_val = recurseArr(subcategories,id);
        if(ret_val){
             return ret_val;   
        }
    }

    return null;
}


console.log(recurseArr(data.categories,10));

但是您可以更有效地保存数据,因为无论如何您都在使用 id - 所以数组使用对象(映射):

var data  = {"categories":
                     {
                        "1":{"parent":0,"name":"Category A","description":"Category description","products":"11","subcategories":{}},
                        "2":{"parent":0,"name":"Category B","description":"Category description","products":"11","subcategories":{
                                "6":{"parent":2,"name":"Subcategory F","description":"Category description", "products":"2","subcategories":{}},
                                "7":{"parent":2,"name":"Subcategory G","description":"Category description","products":"7","subcategories":{}}
                               }
                        }
                     }
};

那么您可以更轻松地访问您的数据。

于 2013-03-29T17:00:32.417 回答