我在javascript中的代码是这样的:

<script type="text/javascript">
    var test = {
        "0": {
            "parent_category_id": "1",
            "parent_category_name": "Asia",
            "id": "4",
            "name": "Japan"
        },
        "1": {
            "parent_category_id": "1",
            "parent_category_name": "Asia",
            "id": "5",
            "name": "Korea"
        },
        "2": {
            "parent_category_id": "1",
            "parent_category_name": "Asia",
            "id": "6",
            "name": "Arab"
        },
        "3": {
            "parent_category_id": "2",
            "parent_category_name": "Europa",
            "id": "7",
            "name": "England"
        },
        "4": {
            "parent_category_id": "2",
            "parent_category_name": "Europa",
            "id": "8",
            "name": "Spain"
        },
        "5": {
            "parent_category_id": "2",
            "parent_category_name": "Europa",
            "id": "9",
            "name": "Italy"
        },
        "6": {
            "parent_category_id": "3",
            "parent_category_name": "America",
            "id": "10",
            "name": "Brazil"
        },
        "7": {
            "parent_category_id": "3",
            "parent_category_name": "America",
            "id": "11",
            "name": "Argentina"
        },
        "8": {
            "parent_category_id": "3",
            "parent_category_name": "America",
            "id": "12",
            "name": "Mexico"
        },
    };
    console.log(test);

</script>


从代码:

parent_category_id = 1、2、3

parent_category_name =亚洲,欧洲,美国

id = 4,5,6,7,8,9,10,11

名称=日本,韩国,阿拉伯,英国,西班牙,意大利,巴西,阿根廷,墨西哥

我想这样显示:

asia
   japan
   korea
   arab
europa
   england
   spain
   italy
america
   brazil
   argentina
   mexico


所以我需要将一维数组转换为二维数组才能显示

我该怎么做?

最佳答案

您可以使用哈希表作为类别的参考。

然后,通过检查哈希表是否具有指定的id来迭代数据并将值分配给同一类别。如果不是,请创建一个新类别,然后将节点推送到结果集。

对于输出,您可以创建一些节点以反映每个项目的嵌套顺序。



function output(array, parent) {
    var ul = document.createElement('ul');
    parent.appendChild(ul);
    array.forEach(function (a) {
        var li = document.createElement('li');
        ul.appendChild(li);
        li.appendChild(document.createTextNode(a.name));
        if (a.children && a.children.length) {
            output(a.children, li);
        }
    });
}

var data = { 0: { parent_category_id: "1", parent_category_name: "Asia", id: "4", name: "Japan" }, 1: { parent_category_id: "1", parent_category_name: "Asia", id: "5", name: "Korea" }, 2: { parent_category_id: "1", parent_category_name: "Asia", id: "6", name: "Arab" }, 3: { parent_category_id: "2", parent_category_name: "Europa", id: "7", name: "England" }, 4: { parent_category_id: "2", parent_category_name: "Europa", id: "8", name: "Spain" }, 5: { parent_category_id: "2", parent_category_name: "Europa", id: "9", name: "Italy" }, 6: { parent_category_id: "3", parent_category_name: "America", id: "10", name: "Brazil" }, 7: { parent_category_id: "3", parent_category_name: "America", id: "11", name: "Argentina" }, 8: { parent_category_id: "3", parent_category_name: "America", id: "12", name: "Mexico" } },
    hash = Object.create(null),
    result = [];

Object.keys(data).forEach(function (k) {
    if (!hash[data[k].parent_category_id]) {
        hash[data[k].parent_category_id] = { id: data[k].parent_category_id, name: data[k].parent_category_name, children: [] };
        result.push(hash[data[k].parent_category_id]);
    }
    hash[data[k].parent_category_id].children.push({ id: data[k].id, name: data[k].name });
});

console.log(result);

output(result, document.body);

关于javascript - 如何基于javascript中的某些键将一维数组转换为二维数组?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46317850/

10-12 23:36