Js N multi-layer (indefinite) nested loop change json key value method outputs a complete new list object

data children is uncertain, and js functions deal with data separately, just like changing name to tile,value to key,
if the children array is empty, then output isLeaf:true, otherwise, continue to cycle to change the subitems
to solve.
the data structure is as follows

list = [
    {
        name: "0",
        value: "0",
        children: [
            name: "0-1",
            value: "0-1",
            children: [
                name: "0-2",
                value: "0-2",
                children: [
                    // ...
                ],
                // ...
            ]
        ]
    },
];
Mar.09,2022

for the moment, it means that the output isLeaf: true is to add this attribute to the data whose children is empty. The code is as follows


function transform(list) {
    if (Array.isArray(list) && list.length !== 0) {
        list = list.map(v => {
            v.title = v.name
            v.key = v.value
            delete v.name
            delete v.value
            if (!transform(v.children)) {
                v.isLeaf = true
            }
            return v
        })
        return list
    } else {
        return false
    }
}

console.log(transform(list))
Menu