猿问

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

            

        }

    }

}

}


沧海一幻觉
浏览 703回答 2
2回答

元芳怎么了

实现了,主要考了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; &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));

慕码人8056858

实现是可以实现,按上楼那种方式,不知道是不是你自己理解画出来的tree,如果是那你应该理解错了,数组转成tree,那里面的对象元素不应该去动啊。如果题目给的就是这个树,那当我没说。
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答