苦苦于重组Json数据结构

时间:2017-09-23 17:33:29

标签: javascript arrays json node.js multidimensional-array

我正在尝试转换此JSON:

[
    [
        {
            "id": 1,
            "ac": 1
        },
        {
            "id": 2,
            "ac": 1
        },
        {
            "id": 3,
            "ac": 1
        }
    ],
    [
        {
            "id": 1,
            "ac": 1
        },
        {
            "id": 2,
            "ac": 1
        },
        {
            "id": 3,
            "ac": 0
        }
    ],
    [
        {
            "id": 1,
            "ac": 1
        },
        {
            "id": 2,
            "ac": 1
        },
        {
            "id": 3,
            "ac": 0
        }
    ],
    [
        {
            "id": 1,
            "ac": 1
        },
        {
            "id": 2,
            "ac": 0
        },
        {
            "id": 3,
            "ac": 0
        }
    ],
    [
        {
            "id": 1,
            "ac": 1
        },
        {
            "id": 2,
            "ac": 0
        },
        {
            "id": 3,
            "ac": 0
        }
    ],
    [
        {
            "id": 1,
            "ac": 1
        },
        {
            "id": 2,
            "ac": 0
        },
        {
            "id": 3,
            "ac": 0
        }
    ]
]

进入该数据结构:

[
    [
        {
            "id": 1,
            "ac": [1,1,1,1,1,1]
        }
    ],
    [
        {
            "id": 2,
            "ac": [1,1,1,0,0,0]
        }
    ],
    [
        {
            "id": 3,
            "ac": [1,0,0,0,0,0]
        }
    ]
]

我正在使用JavaScript / node这样做,但我无法让它工作。任何人都可以帮我写一个可以这样做的循环吗?

编辑: 我尝试的是,以下,但我认为它没有多大帮助,因为它仍然不是我需要的输出

var acc_array = [];
var a_id;
for(var y=0; y<data.length; y++) { //151
    if(y<data.length-1){
        a_id = data[y].a_id;
        if(a_id == data[y].a_id) {
            acc_array[y] = new Array();
            acc_array[y].push(data[y].acc);
        }
    }
    if(y==data.length-1){
        acc_array[y] = new Array();
        a_id = data[y].a_id;
        if(a_id == data[y].a_id) {
            acc_array[y].push(data[y].acc);
        }
        jsonfile.writeFile("a.json", acc_array, function (err) {
            console.log(err);
        });
    }
}

2 个答案:

答案 0 :(得分:0)

您可以使用嵌套循环和哈希表来收集具有相同id的项目。

&#13;
&#13;
var data = [[{ id: 1, ac: 1 }, { id: 2, ac: 1 }, { id: 3, ac: 1 }], [{ id: 1, ac: 1 }, { id: 2, ac: 1 }, { id: 3, ac: 0 }], [{ id: 1, ac: 1 }, { id: 2, ac: 1 }, { id: 3, ac: 0 }], [{ id: 1, ac: 1 }, { id: 2, ac: 0 }, { id: 3, ac: 0 }], [{ id: 1, ac: 1 }, { id: 2, ac: 0 }, { id: 3, ac: 0 }], [{ id: 1, ac: 1 }, { id: 2, ac: 0 }, { id: 3, ac: 0 }]],
    hash = Object.create(null),
    result = [];

data.forEach(function (a) {
    a.forEach(function (o) {
        hash[o.id] || result.push(hash[o.id] = { id: o.id, ac: [] });
        hash[o.id].ac.push(o.ac);
    });
});

console.log(result);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;
&#13;
&#13;

答案 1 :(得分:0)

Array#将所有内容连接到一个数组,然后使用带有辅助对象的Array#reduce来获取所需对象的数组:

&#13;
&#13;
var data = [[{"id":1,"ac":1},{"id":2,"ac":1},{"id":3,"ac":1}],[{"id":1,"ac":1},{"id":2,"ac":1},{"id":3,"ac":0}],[{"id":1,"ac":1},{"id":2,"ac":1},{"id":3,"ac":0}],[{"id":1,"ac":1},{"id":2,"ac":0},{"id":3,"ac":0}],[{"id":1,"ac":1},{"id":2,"ac":0},{"id":3,"ac":0}],[{"id":1,"ac":1},{"id":2,"ac":0},{"id":3,"ac":0}]];

var hash = Object.create(null);
var result = [].concat(...data)
  .reduce(function(r, o) {
    var item = hash[o.id];
    
    if(!item) {
      item = hash[o.id] = { id: o.id, ac: [] };    
      r.push(item);
    }
    
    item.ac.push(o.ac);
    
    return r;
  }, []);
  
console.log(result);
&#13;
&#13;
&#13;