Asking for help: a question about Recursive formatting data

now you need a tree menu (it needs to be automatically generated according to the data situation, not online, and in extreme cases, there are many level menus). Instead of using plug-ins, you need the following data format

[
    {
        name: "1",
        children: [
            {
                name: "1-1",
                children: [
                    {
                        name: "1-1-1"
                    }
                ]
            }
        ]
    },
    {
        name: "2",
        children: [
            {
                name: "2-2"
            }
        ]
    },
    {
        name: "3"
    }
]

// childrenchildren

the data format provided by the background is as follows

[
    {
        name: "",
        children: {
            "aaa":{
                name: "aaa"
            },
            "bbb": {
                name: "bbb",
                children: {
                    "ccc": {
                        name: "ccc"
                    },
                    "ddd": {
                        name: "ddd"
                    }
                }
            }
        }
    },
    {
        name: "2",
        children: {
            "eee": {
                name: "eee"
            }
        }
    },
    {
        name: "3"
    }
]


// ,childrenjsonkey,json{name: ""}

the semi-finished method written by yourself is as follows

var treeData = [];
function createdTreeData(data) {
  data.forEach(function (item) {
    function children(childrenData) {
      var childrenArr = []
      for (key in childrenData) {
        // for in 
      }
      return childrenArr
    }
    treeData.push({
      name: item.name,
      children: children(item.children)
    })
  })
}
// ps:ES6babel

wait online, thank you

Apr.28,2022

emmmm...

function handleChildren(obj) {
  return Object.keys(obj).map(function(v) {
    var children = obj[v].children;
    if (children) obj[v].children = handleChildren(children);
    return obj[v];
  });
}
function createdTreeData(arr) {
  return arr.map(function(v) {
    var children = v.children;
    if (children) v.children = handleChildren(children);
    return v;
  });
}
createdTreeData(arr);

//
var nemuData1 = [
    {
        name: '',
        children: {
            'aaa':{
                name: 'aaa'
            },
            'bbb': {
                name: 'bbb',
                children: {
                    'ccc': {
                        name: 'ccc'
                    },
                    'ddd': {
                        name: 'ddd'
                    }
                }
            }
        }
    },
    {
        name: '2',
        children: {
            'eee': {
                name: 'eee'
            }
        }
    },
    {
        name: '3'
    }
]
//
var nemuData2 = []

//
function formatData(data, parent){
    if(parent){
        for(var key in data) {
            var obj2 = {
                name: data[key].name,
                children: []
            }
            if(data[key].children){
                formatData(data[key].children, obj2)
            }
            parent.children.push(obj2)
        }
    }else {
        data.forEach(function (item) {
            var obj1 = {
                name: item.name,
                children: []
            }
            if(item.children){
                formatData(item.children, obj1)
            }
            nemuData2.push(obj1)
        })
        
    }
}
formatData(nemuData1)
console.log(nemuData2)

var tree = [
    {
        name: '',
        children: {
            'aaa':{
                name: 'aaa'
            },
            'bbb': {
                name: 'bbb',
                children: {
                    'ccc': {
                        name: 'ccc'
                    },
                    'ddd': {
                        name: 'ddd'
                    }
                }
            }
        }
    },
    {
        name: '2',
        children: {
            'eee': {
                name: 'eee'
            }
        }
    },
    {
        name: '3'
    }
];
var makeTree = function(data) {
    if (data instanceof Array) return data.map(function(n) { return makeTree(n); });
    else if (typeof data === "object") {
        if ("name" in data) {
            var obj = { name: data.name };
            "children" in data && (obj.chilren = makeTree(data.children));
            return obj;
        } else return Object.keys(data).map(function(k) { return makeTree(data[k]); });
    }
};
console.log(makeTree(tree));
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-1b326f3-2be2b.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-1b326f3-2be2b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?