Js converts the received array to the desired format

has the following format

{
    "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"
            }
        }
    }
}

needs to be converted to this format

[{
        "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"
            }
        ]
    }
]

requires that the value of the new data corresponds to the id of the original data. The novice debugged it for a whole day without any effect. Seek Daniel"s solution.

Mar.11,2021

it's messy. Please refer to

.
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);

traverse the tree and regenerate the array. The front end does this work, which consumes a lot of performance. Especially when there is a large amount of data, it may get stuck. Generally, we let the back end deal with it and then give it to us

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3110c-2bd59.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3110c-2bd59.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?