鍍金池/ 問答/數(shù)據(jù)分析&挖掘  HTML/ js 將接收到的數(shù)組轉(zhuǎn)換成需要的格式

js 將接收到的數(shù)組轉(zhuǎn)換成需要的格式

有如下這種格式

{
    "2": {
        "id": 2,
        "name": "demo1",
        "lv1": {
            "4": {
                "id": 20,
                "name": "demo1-1",
                "lv2": {
                    "2": {
                        "id": 2,
                        "name": "demo1-1-1"
                    },
                    "3": {
                        "id": 3,
                        "name": "demo1-1-2"
                    }
                }
            },
            "7": {
                "id": 23,
                "name": "demo1-2"
            }
        }

    },

    "3": {
        "id": 4,
        "name": "demo2",
        "lv1": {
            "6": {
                "id": 34,
                "name": "demo2-1",
                "lv2": {
                    "3": {
                        "id": 3,
                        "name": "demo2-1-1"
                    }
                }
            },
            "8": {
                "id": 55,
                "name": "demo2-2"
            }
        }
    }
}

需要轉(zhuǎn)換成這種格式

[{
        "value": "2",
        "text": "demo1",
        "children": [{
            "value": "20",
            "text": "demo1-1",
            "children": [{
                    "value": "2",
                    "text": "demo1-1-1"
                },
                {
                    "value": "3",
                    "text": "demo1-1-2"
                }
            ]
        }]
    },
    {
        "value": "4",
        "text": "demo2",
        "children": [{
                "value": "34",
                "text": "demo2-1",
                "children": [{
                    "value": "3",
                    "text": "demo2-1-1"
                }]
            },
            {
                "value": "55",
                "text": "demo2-2"
            }
        ]
    }
]

要求新數(shù)據(jù)value跟原數(shù)據(jù)id是對應(yīng)關(guān)系,新手調(diào)試了一整天都沒效果,求大牛解答。

回答
編輯回答
尤禮

對樹遍歷,重新生成數(shù)組,前端做這工作,挺耗性能的,尤其數(shù)據(jù)量龐大的時候,可能會卡死,一般我們都讓后端處理好再給我們

2018年3月23日 22:08
編輯回答
傻丟丟

比較亂, 可以參考下:

var data = {
    "2": {
        "id": 2,
        "name": "demo1",
        "lv1": {
            "4": {
                "id": 20,
                "name": "demo1-1",
                "lv2": {
                    "2": {
                        "id": 2,
                        "name": "demo1-1-1"
                    },
                    "3": {
                        "id": 3,
                        "name": "demo1-1-2"
                    }
                }
            },
            "7": {
                "id": 23,
                "name": "demo1-2"
            }
        }

    },

    "3": {
        "id": 4,
        "name": "demo2",
        "lv1": {
            "6": {
                "id": 34,
                "name": "demo2-1",
                "lv2": {
                    "3": {
                        "id": 3,
                        "name": "demo2-1-1"
                    }
                }
            },
            "8": {
                "id": 55,
                "name": "demo2-2"
            }
        }
    }
};
function fn (obj) {
    var arr = [];
    for(var key in obj) {
        var child = null;
        for(var subKey in obj[key]) {
            if(/lv/.test(subKey)) {
                child = fn(obj[key][subKey]);
            }
        }
        if(child != null) {
            arr.push(
                {
                    "value": key,
                    "text": obj[key].name,
                    "children": child
                }
            );
        } else {
            arr.push(
                {
                    "value": key,
                    "text": obj[key].name
                }
            );
        }
    }
    return arr;
}

fn(data);
2017年10月26日 07:57