UnderscoreJS - 将多个数组合并为一个

UnderscoreJS - Merge multiple arrays into one

我有四个数组,如下所示:

var array1 = [['dept', 'dept1'], ['empl', 'empl1'], ['month', 'Jan']];
var array2 = [['dept', 'dept2'], ['empl', 'empl2'], ['month', 'Feb']];
var array3 = [['dept', 'dept3'], ['empl', 'empl3'], ['month', 'March']];
var array4 = [['dept', 'dept4'], ['empl', 'empl4'], ['month', 'April']];

并使用 Underscore.js,我想合并它们以重现结果数组:

var resultArray = [
  ['dept', 'dept1', 'dept2', 'dept3', 'dept4'], 
  ['empl', 'empl1', 'empl2', 'empl3', 'empl4'], 
  ['month', 'Jan', 'Feb', 'March', 'April']
];

所以这里每个嵌套数组的第一个元素是 Key

我尝试使用 _.zip 但它会重现单独的数组。

这是相同的 JSFiddle

编辑

其中一个数组中可能缺少某些元素。例如:

var array1 = [['dept', 'dept1'], ['empl', 'empl1'], ['month', 'Jan']];
var array2 = [['empl', 'empl2'], ['month', 'Feb']];
var array3 = [['dept', 'dept3'], ['month', 'March']];
var array4 = [['dept', 'dept4'], ['empl', 'empl4']];
 const result = [], position = {};

 for(const array of [array1, array2, array3, array4]){
   for(const [key, value] of array){
      if(key in position){
        result[ position[key] ].push(value);
      } else {
        position[key] = result.push([key, value]) - 1;
      }
   }
 }

这使用哈希表来存储键的位置。当键还没有被使用时,它会将一个新的数组推送到包含键和值的结果中,如果数组已经存在,它只是将值推送到该位置下的数组。请注意,这是 O(n),其中 n 是键值对的数量。


如果你想要一个稀疏数组:

 const result = [], position = {};

 for(const [index, array] of [array1, array2, array3, array4].entries()){
   for(const [key, value] of array){
      if(!(key in position))
        position[key] = result.push([key]) - 1;
      result[ position[key] ][index + 1] = value;            
   }
 }

你真的需要 Underscore JS 吗?

//输入

var array1 = [['dept', 'dept1'], ['empl', 'empl1'], ['month', 'Jan']];
var array2 = [['dept', 'dept2'], ['empl', 'empl2'], ['month', 'Feb']];
var array3 = [['dept', 'dept3'], ['empl', 'empl3'], ['month', 'March']];
var array4 = [['dept', 'dept4'], ['empl', 'empl4'], ['month', 'April']];

//直接方式

var resultArray = [
Array.from(new set([...array1[0],...array2[0],...array3[0],...array4[0]])),
Array.from(new Set([...array1[1],...array2[1],...array3[1],...array4[1]])),
Array.from(new Set([...array1[2],...array2[2],...array3[2],...array4[2]]))];

//循环

var resultArray = [];
for(let i = 0; i <3; i++){
    resultArray.push(Array.from(new Set([...array1[i],...array2[i],...array3[i],...array4[i]])));
}

//结果

[
  ['dept', 'dept1', 'dept2', 'dept3', 'dept4'], 
  ['empl', 'empl1', 'empl2', 'empl3', 'empl4'], 
  ['month', 'Jan', 'Feb', 'March', 'April']
]

您可以将您的数组组合成一个数组,然后使用 array#reducearray#forEach 您可以使用每个数组的第一个值合并它们。

var array1 = [['dept', 'dept1'], ['empl', 'empl1'], ['month', 'Jan']];
var array2 = [['empl', 'empl2'], ['month', 'Feb']];
var array3 = [['dept', 'dept3'], ['month', 'March']];
var array4 = [['dept', 'dept4'], ['empl', 'empl4']];
var combined = []
combined.push(array1, array2, array3, array4);
var result = Object.values(combined.reduce((r,a) => {
   a.forEach(([name, value]) => {
    r[name] = r[name] || [name];
    r[name].push(value);
   });
   return r;
 }, {}))
console.log(result);