用相同的键组合多个数组

时间:2017-07-11 15:49:15

标签: javascript underscore.js

我尝试按特定的fruit/apple.txt fruit/banana.txt 属性组合多个数组。例如,我有

ARR1

key

ARR2

  [{
    key: 'A',
    items: [{name: 'a item'}]
  }, {
    key: 'B',
    items: [{name: 'b item'}]
  }]

如何制作以下 arr3

  [{
    key: 'B',
    items: [{name: 'another b item'}, {name: 'more b items'}, {name: 'even more b items'}]
  }]

3 个答案:

答案 0 :(得分:2)

可以使用这样的哈希表:

arr1=[{
    key: 'A',
    items: [{name: 'a item'}]
  }, {
    key: 'B',
    items: [{name: 'b item'}]
  }];

arr2=[{
    key: 'B',
    items: [{name: 'another b item'}, {name: 'more b items'}, {name: 'even more b items'}]
  }];
  
console.log(arr1.concat(arr2).reduce((function(hash){
   return function(array,obj){
    if(!hash[obj.key])
     array.push(hash[obj.key]=obj);
    else
       hash[obj.key].items.push(...obj.items);
    return array;
   };    

})({}),[]));

一些解释:

arr1.concat(arr2)//just work with one array as its easier
.reduce(...,[]));//reduce this array to the resulting array
//through
(function(hash){//an IIFE to closure our hash object
...
})({})
//which evaluates to
  function(array,obj){//take the resulting array and one object of the input
    if(!hash[obj.key])//if we dont have the key yet
     array.push(hash[obj.key]=obj);//init the object and add to our result
    else
       hash[obj.key].items.push(...obj.items);//simply concat the items
    return array;
  };    

答案 1 :(得分:0)

我觉得我曾经多次使用类似的片段。更改了用例的命名。

var output = array.reduce(function(o, cur) {

  // Get the index of the key-value pair.
  var occurs = o.reduce(function(k, item, i) {
    return (item.key === cur.key) ? i : k;
  }, -1);

  // If the name is found,
  if (occurs >= 0) {

    // append the current value to its list of values.
    o[occurs].item = o[occurs].value.concat(cur.items);

  // Otherwise,
  } else {

    // add the current item to o (but make sure the value is an array).
    var obj = {key: cur.key, item: [cur.items]};
    o = o.concat([obj]);
  }

  return o;
}, []);

答案 2 :(得分:0)



var arr1 = [{
    key: 'A',
    items: [{name: 'a item'}]
  }, {
    key: 'B',
    items: [{name: 'b item'}]
  }]
var arr2 =  [{
    key: 'B',
    items: [{name: 'another b item'}, {name: 'more b items'}, {name: 'even more b items'}]
  }]
  
  
var arr3 = _.map(_.groupBy(arr1.concat(arr2), 'key'), 
  val => ({
    key : val[0].key, items: _.union.apply(this,val.map(v => v.items))
    })
)
console.log(arr3)

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
&#13;
&#13;
&#13;