js递归数据操作,非常复杂,无法解决。

1:因为和用的angular,输出传回时,需要满足后台要求的数据格式。在前台操作的时候,为了满足primeng的使用数据格式,导致数据格式不同,求大神帮忙写一下,已经耗费了一天时间。
2:这是我现在使用的数据,需要可以无限嵌套下去!

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": "二",
            
        }
    }
];

这是后台需要的数据格式。

  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:搞了一天了,还是没有解决掉,跪求帮忙。

阅读 2.2k
1 个回答
let map = item => {
    var data = {};
    Object.keys(item['data']).forEach(key => data[key] = item['data'][key]);
    // data['children'] = (item['children'] || []).map(map);
    // 才发现你目标数据结构里要保留 null,改一下。
    data['children'] = item['children'] ? item['children'].map(map) : null;
    return data;
}

let resArr = arr.map(map);

测试了一下应该可以。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题