使用Underscorejs合并多维数组

时间:2014-05-17 03:58:31

标签: underscore.js backbone-collections

[{"ic_value1":"PTP-1","in_id":"80"},{"ic_value1":"PTP-2","in_id":"81"},{"ic_value1":"PTP-3A","in_id":"82"},{"ic_value1":"PTP-3B","in_id":"83"},{"ic_value1":"PTP-4","in_id":"84"},{"ic_value1":"PTP-5A","in_id":"85"},{"ic_value1":"PTP-5B","in_id":"86"},{"ic_value1":"PTP-6","in_id":"87"},{"ic_value1":"PTP-7","in_id":"88"},{"ic_value1":"PTP-8","in_id":"89"},{"ic_value1":"PTP-9A","in_id":"90"},{"ic_value1":"PTP-9B","in_id":"91"},{"ic_value1":"PTP-10","in_id":"92"},{"ic_value1":"PTP-11","in_id":"93"},{"ic_value1":"PTP-12A","in_id":"94"},{"ic_value1":"PTP-13","in_id":"95"},{"ic_value1":"PTP-13","in_id":"96"},{"ic_value1":"PTP-14","in_id":"97"},{"ic_value1":"PTP-15","in_id":"98"},{"ic_value1":"PTP-16","in_id":"99"},{"ic_value1":"DH-129","in_id":"100"},{"ic_value1":"DH-130","in_id":"101"}]

[{"ic_value1":"09+75","in_id":"80"},{"ic_value1":"09+75","in_id":"81"},{"ic_value1":"09+09","in_id":"82"},{"ic_value1":"09+09","in_id":"83"},{"ic_value1":"09+06","in_id":"84"},{"ic_value1":"08+08","in_id":"85"},{"ic_value1":"08+08","in_id":"86"},{"ic_value1":"08+08","in_id":"87"},{"ic_value1":"08+00","in_id":"88"},{"ic_value1":"07+10","in_id":"89"},{"ic_value1":"06+00","in_id":"90"},{"ic_value1":"06+00","in_id":"91"},{"ic_value1":"04+43","in_id":"92"},{"ic_value1":"10+99","in_id":"93"},{"ic_value1":"10+83","in_id":"94"},{"ic_value1":"03+84","in_id":"95"},{"ic_value1":"03+84","in_id":"96"},{"ic_value1":"04+52","in_id":"97"},{"ic_value1":"03+94","in_id":"98"},{"ic_value1":"04+54","in_id":"99"},{"ic_value1":"07+82","in_id":"100"},{"ic_value1":"07+42","in_id":"101"}] 

[{"ic_value1":"112D","in_id":"80"},{"ic_value1":"190D","in_id":"81"},{"ic_value1":"096D","in_id":"82"},{"ic_value1":"096D","in_id":"83"},{"ic_value1":"184D","in_id":"84"},{"ic_value1":"092D","in_id":"85"},{"ic_value1":"092D","in_id":"86"},{"ic_value1":"190D","in_id":"87"},{"ic_value1":"040D","in_id":"88"},{"ic_value1":"040D","in_id":"89"},{"ic_value1":"010D","in_id":"90"},{"ic_value1":"010D","in_id":"91"},{"ic_value1":"017D","in_id":"92"},{"ic_value1":"060D","in_id":"93"},{"ic_value1":"020D","in_id":"94"},{"ic_value1":"007D","in_id":"95"},{"ic_value1":"007D","in_id":"96"},{"ic_value1":"024D","in_id":"97"},{"ic_value1":"048D","in_id":"98"},{"ic_value1":"050D","in_id":"99"},{"ic_value1":"095D","in_id":"100"},{"ic_value1":"080D","in_id":"101"}] 

如何将这三个数组对齐成行和列。每个包含相同in_id的第一个项目,我想将其添加到行和列

预期产出:

[{80: ["PTP-1","09+75","112D"]]
像这样的类似的东西

如何使用underscorejs实现它

1 个答案:

答案 0 :(得分:0)

由于id似乎以相同的顺序出现在三个数组中的每一个中,所以它就像

一样简单
_.map(_.zip(array1, array2, array3), function(row) {
    var o = {};
    o[row[0].in_id] = _.pluck(row, "ic_value1");
    return o;
});