如何基于javascript中的某些键将1维数组转换为2维数组?

时间:2017-09-20 09:10:21

标签: javascript jquery arrays

我在javascript中的代码如下:


Line 24: _IGNORED_PREFIX = ('.', 'node_modules')

来自代码:

parent_category_id = 1,2,3

parent_category_name =亚洲,欧洲,美国

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

name = japan,korea,arab,england,spain,italy,brazil,argentina,mexico

我想这样显示:

<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>

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

我该怎么做?

3 个答案:

答案 0 :(得分:1)

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

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

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

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

各国应该是一个数组,因为它没有相关的密钥。

&#13;
&#13;
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"
        }
};
   
var _formatted = {};

for(obj in test){
     
    var region = test[obj].parent_category_name;
    if(typeof _formatted[region] === "undefined"){
        _formatted[region] = [];
    }    
    _formatted[region].push(test[obj].name);
}

console.log(_formatted);
&#13;
&#13;
&#13;

答案 2 :(得分:0)

针对您的具体情况:

var categories = []
for (t in test){
    if(! (foundCategory = categories.find(cat => cat.id ===  test[t].parent_category_id))){
        // this category is not in our array
        // add it
        categories.push({
            id: test[t].parent_category_id,
            name: test[t].parent_category_name,
            subcategories: [{
                id: test[t].id,
                name: test[t].name
            }]
        })
  }
  else{
    // category is already in our array
    // push the subcategory
    foundCategory.subcategories.push({
      id: test[t].id,
      name: test[t].name
    })
}

}