JS面试题-计算题,写个函数,把数组变成对象结构

请问,下面 data数组,如何写一个函数,使 data数组变成 tree的格式结构。
var data = [
{id:"2", value:"xxx"},
{id:"3", value:"xxx"},
{id:"4", value:"xxx"},
{id:"6", value:"xxx"},
{id:"5", value:"xxx"},
{id:"1", value:"xxx"},
]
写个函数变为
var tree = {

"1":{

    "children":{

        "2":{

            "children":{

                "4":{

                    "children":{

                        "6":{

                        

                        }

                    }

                }

            }

        }

    }

},

"3":{

    "children":{

        "5":{

            

        }

    }

}


慕工程0101907
浏览 963回答 2
2回答

米脂

大兄弟,你这tree完全没有规律啊

慕沐林林

实现了,主要考了3点:逻辑递归Object.keys()代码未优化,先去吃饭了,本地测试完成var data = [&nbsp; &nbsp; { id: "2", value: "xxx" },&nbsp; &nbsp; { id: "3", value: "xxx" },&nbsp; &nbsp; { id: "4", value: "xxx" },&nbsp; &nbsp; { id: "6", value: "xxx" },&nbsp; &nbsp; { id: "5", value: "xxx" },&nbsp; &nbsp; { id: "1", value: "xxx" },];var tree = {&nbsp; &nbsp; "1": {&nbsp; &nbsp; &nbsp; &nbsp; "children": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "2": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "children": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "4": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "children": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "6": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; "3": {&nbsp; &nbsp; &nbsp; &nbsp; "children": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "5": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }};function trans(data) {&nbsp; &nbsp; let dataSort = data.sort((a, b) => {&nbsp; &nbsp; &nbsp; &nbsp; return a.id - b.id < 0 ? -1 : 1;&nbsp; &nbsp; });&nbsp; &nbsp; let tree;&nbsp; &nbsp; for (let i = 0; i < dataSort.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; let idNum = dataSort[i].id;&nbsp; &nbsp; &nbsp; &nbsp; let isEven = Number(idNum) % 2 === 0;&nbsp; &nbsp; &nbsp; &nbsp; if (tree === undefined) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tree = {};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tree[1] = {};&nbsp; &nbsp; &nbsp; &nbsp; } else if (isEven) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; appendChildAttr(tree[1], idNum);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (tree[3] === undefined) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tree[3] = {};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; appendChildAttr(tree[3], idNum); &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; function appendChildAttr(parNode, childrenId) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Object.keys(parNode).length === 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parNode.children = {};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parNode.children[childrenId] = {};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; appendChildAttr(parNode.children[Object.keys(parNode.children)[0]], childrenId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return tree;}console.log(JSON.stringify(trans(data))===JSON.stringify(tree));
打开App,查看更多内容
随时随地看视频慕课网APP