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 に答える