鍍金池/ 問答/HTML5  HTML/ js遞歸數(shù)據(jù)操作,非常復雜,無法解決。

js遞歸數(shù)據(jù)操作,非常復雜,無法解決。

1:因為和用的angular,輸出傳回時,需要滿足后臺要求的數(shù)據(jù)格式。在前臺操作的時候,為了滿足primeng的使用數(shù)據(jù)格式,導致數(shù)據(jù)格式不同,求大神幫忙寫一下,已經(jīng)耗費了一天時間。
2:這是我現(xiàn)在使用的數(shù)據(jù),需要可以無限嵌套下去!

var arr = [ {"children": [

            {
                "children": [
                    {
                        "children": [
                            {
                                "children": [
                                    {
                                        "children": null,
                                        "data": {
                                            "data": {
                                                "start": null,
                                                "limit": null,
                                                "orderCol": null,
                                                "zdxmc": "五",
                                        
                                            }
                                        }
                                    }
                                ],
                                "data": {
                                    "start": null,
                                    "limit": null,
                                    "orderCol": null,
                                    "zdxmc": "四",
                       
                                }
                            }
                        ],
                        "data": {
                            "start": null,
                            "limit": null,
                            "orderCol": null,
                            "zdxmc": "三",
                     
                        }
                    }
                ],
                "data": {
                    "start": null,
                    "limit": null,
                    "orderCol": null,
                    "zdxmc": "er",
           
                }
            }
        ],
        "data": {
            "start": null,
            "limit": null,
            "orderCol": null,
            "zdxmc": "一",
    
        }
    },
    {
        "children": [
            {
                "children": [
                    {
                        "children": [
                            {
                                "children": [
                                    {
                                        "children": null,
                                        "data": {
                                            "data": {
                                                "start": null,
                                                "limit": null,
                                                "orderCol": null,
                                                "zdxmc": "五",
                                    
                                            }
                                        }
                                    }
                                ],
                                "data": {
                                    "start": null,
                                    "limit": null,
                                    "orderCol": null,
                                    "zdxmc": "四",
                       
                                }
                            }
                        ],
                        "data": {
                            "start": null,
                            "limit": null,
                            "orderCol": null,
                            "zdxmc": "三",
                   
                        }
                    }
                ],
                "data": {
                    "start": null,
                    "limit": null,
                    "orderCol": null,
                    "zdxmc": "er",
                   
                }
            }
        ],
        "data": {
            "start": null,
            "limit": null,
            "orderCol": null,
            "zdxmc": "二",
            
        }
    }
];

這是后臺需要的數(shù)據(jù)格式。

  var resArr = [
    {
        "start": null,
        "limit": null,
        "orderCol": null,
        "zdxmc": "一",
       
        "children": [
            {
                "start": null,
                "limit": null,
                "orderCol": null,
                "zdxmc": "er",
             
                "children": [
                    {
                        "start": null,
                        "limit": null,
                        "orderCol": null,
                        "zdxmc": "三",
                       
                        "children": [
                            {
                                "start": null,
                                "limit": null,
                                "orderCol": null,
                                "zdxmc": "四",
                           
                                "children": [
                                    {
                                        "data": {
                                            "start": null,
                                            "limit": null,
                                            "orderCol": null,
                                            "zdxmc": "五",
                                   
                                        },
                                        "children": null
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "start": null,
        "limit": null,
        "orderCol": null,
        "zdxmc": "二",
 
        "children": [
            {
                "start": null,
                "limit": null,
                "orderCol": null,
                "zdxmc": "er",
             
                "children": [
                    {
                        "start": null,
                        "limit": null,
                        "orderCol": null,
                        "zdxmc": "三",
                 
                        "children": [
                            {
                                "start": null,
                                "limit": null,
                                "orderCol": null,
                                "zdxmc": "四",
                           
                                "children": [
                                    {
                                        "data": {
                                            "start": null,
                                            "limit": null,
                                            "orderCol": null,
                                            "zdxmc": "五",
                                       
                                        },
                                        "children": null
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
];

3:搞了一天了,還是沒有解決掉,跪求幫忙。

回答
編輯回答
誮惜顏
let map = item => {
    var data = {};
    Object.keys(item['data']).forEach(key => data[key] = item['data'][key]);
    // data['children'] = (item['children'] || []).map(map);
    // 才發(fā)現(xiàn)你目標數(shù)據(jù)結構里要保留 null,改一下。
    data['children'] = item['children'] ? item['children'].map(map) : null;
    return data;
}

let resArr = arr.map(map);

測試了一下應該可以。

2017年9月29日 12:56