4

我从服务器获取数据列表,我必须对其进行转换。其中一部分是将其转换为 3 维数组。在“myArr[i].children.push(temp);”之后 它留下了被推入数组根目录的对象的副本。我可以在不复制的情况下推送还是如何删除这些?(我有下划线js,我知道他们有很好的数组函数:))

for (var i = 0; i < myArr.length; i++) {
    myArr[i].children = [];
    for (var q = 0; q < myArr.length; q++) {
        if (myArr[i].id == myArr[q].parentid) {
            var temp = {
                id: myArr[q].id,
                index: myArr[q].index,
                text: myArr[q].text
            }
            myArr[i].children.push(temp);
        };
    };  
};

数据

[{
    "id": "5",
    "parentid": "0",
    "text": "Device Guides",
    "index": "0"
}, {
    "id": "6",
    "parentid": "0",
    "text": "Pre-Sales Evaluation",
    "index": "1"
}, {
    "id": "7",
    "parentid": "0",
    "text": "Router Setup Guides",
    "index": "2"
}, {
    "id": "9",
    "parentid": "7",
    "text": "Sonicwall",
    "index": "0"
}, {
    "id": "10",
    "parentid": "5",
    "text": "Grandstream GXP-21XX",
    "index": "1"
}, {
    "id": "11",
    "parentid": "5",
    "text": "Polycom Soundstation\/Soundpoint",
    "index": "2"
}, {
    "id": "12",
    "parentid": "7",
    "text": "Cisco",
    "index": "1"
}, {
    "id": "15",
    "parentid": "0",
    "text": "Post-Sales Implementation Check List",
    "index": "7"
}, {
    "id": "16",
    "parentid": "15",
    "text": "Porting and New Number Details",
    "index": "0"
}, {
    "id": "18",
    "parentid": "15",
    "text": "Partner Setup",
    "index": "1"
}, {
    "id": "19",
    "parentid": "15",
    "text": "test",
    "index": "2"
}, {
    "id": "20",
    "parentid": "0",
    "text": "test",
    "index": "11"
}, {
    "id": "21",
    "parentid": "15",
    "text": "test",
    "index": "3"
}, {
    "id": "23",
    "parentid": "5",
    "text": "New Polycom",
    "index": "0"
}, {
    "id": "24",
    "parentid": "0",
    "text": "Test Markup",
    "index": "14"
}, {
    "id": "25",
    "parentid": "0",
    "text": "test",
    "index": "15"
}]

形成后:

{
    "children": [{
        "id": "5",
        "parentid": "0",
        "text": "Device Guides",
        "index": "1",
        "children": [{
            "id": "10",
            "index": "0",
            "text": "Grandstream GXP-21XX"
        }, {
            "id": "11",
            "index": "1",
            "text": "Polycom Soundstation/Soundpoint"
        }, {
            "id": "23",
            "index": "2",
            "text": "New Polycom"
        }]
    }, {
        "id": "6",
        "parentid": "0",
        "text": "Pre-Sales Evaluation",
        "index": "0",
        "children": []
    }, {
        "id": "7",
        "parentid": "0",
        "text": "Router Setup Guides",
        "index": "2",
        "children": [{
            "id": "9",
            "index": "0",
            "text": "Sonicwall"
        }, {
            "id": "12",
            "index": "1",
            "text": "Cisco"
        }]
    }, {
        "id": "9",
        "parentid": "7",
        "text": "Sonicwall",
        "index": "0",
        "children": []
    }, {
        "id": "10",
        "parentid": "5",
        "text": "Grandstream GXP-21XX",
        "index": "0",
        "children": []
    }, {
        "id": "11",
        "parentid": "5",
        "text": "Polycom Soundstation/Soundpoint",
        "index": "1",
        "children": []
    }, {
        "id": "12",
        "parentid": "7",
        "text": "Cisco",
        "index": "1",
        "children": []
    }, {
        "id": "15",
        "parentid": "0",
        "text": "Post-Sales Implementation Check List",
        "index": "7",
        "children": [{
            "id": "16",
            "index": "0",
            "text": "Porting and New Number Details"
        }, {
            "id": "18",
            "index": "1",
            "text": "Partner Setup"
        }, {
            "id": "19",
            "index": "2",
            "text": "test"
        }, {
            "id": "21",
            "index": "3",
            "text": "test"
        }]
    }, {
        "id": "16",
        "parentid": "15",
        "text": "Porting and New Number Details",
        "index": "0",
        "children": []
    }, {
        "id": "18",
        "parentid": "15",
        "text": "Partner Setup",
        "index": "1",
        "children": []
    }, {
        "id": "19",
        "parentid": "15",
        "text": "test",
        "index": "2",
        "children": []
    }, {
        "id": "20",
        "parentid": "0",
        "text": "test",
        "index": "11",
        "children": []
    }, {
        "id": "21",
        "parentid": "15",
        "text": "test",
        "index": "3",
        "children": []
    }, {
        "id": "23",
        "parentid": "5",
        "text": "New Polycom",
        "index": "2",
        "children": []
    }, {
        "id": "24",
        "parentid": "0",
        "text": "Test Markup",
        "index": "14",
        "children": []
    }, {
        "id": "25",
        "parentid": "0",
        "text": "test",
        "index": "15",
        "children": []
    }]
}
4

2 回答 2

5

干得好

tree = {0: {children: []}}

data.forEach(function(x) {
    x.children = tree[x.id] ? tree[x.id].children : [];
    tree[x.id] = x;

    if(!tree[x.parentid])
        tree[x.parentid] = {children: []}
    tree[x.parentid].children.push(x)
})

result = tree[0].children

该解决方案是线性的(仅在数组上迭代一次)并且不需要任何预排序。

http://jsfiddle.net/U47WY/

以下是将树转换回线性数组的方法:

function flatten(source) {
    return source.reduce(function(a, x) {
        var children = x.children;
        delete x.children;
        return a.concat([x], flatten(x.children))
    }, []);
}
于 2013-04-16T14:19:43.083 回答
2

继评论中的友好讨论之后:

var zeroObj = {"children":[]};
for (var i = 0; i < myArr.length; i++) {
    if(myArr[i].parentid === 0) {
        zeroObj.children.push(myArr[i]);
    } else {
        for (var q = 0; q < myArr.length; q++) {
            if (myArr[i].parentid == myArr[q].id) {
                myArr[q].children = myArr[q].children || [];
                myArr[q].children.push(myArr[i]);
            };
        }; 
    }
};
于 2013-04-16T14:23:39.447 回答