如何使用lodash在对象数组中合并/连接相同对象属性的值? [英] How to merge/concatenate values of same object properties in an array of objects using lodash?

查看:1011
本文介绍了如何使用lodash在对象数组中合并/连接相同对象属性的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象数组,看起来像这样:

I have an array of arrays of objects which looks like this:

let fruitSamples = [
  [
    {'id': 1,'type': 'apples','samples': [1, 2, 3]},
    {'id': 2,'type': 'bananas','samples': [1, 2, 7]},
    {'id': 3,'type': 'pears','samples': [1, 2, 3]}
  ],
  [
    {'id': 1,'type': 'apples','samples': [5, 2, 9]},
    {'id': 2,'type': 'bananas','samples': [1, 7, 7]},
    {'id': 3,'type': 'pears','samples': [12, 21, 32]}
  ],
  [
    {'id': 1,'type': 'apples','samples': [11, 2, 33]},
    {'id': 2,'type': 'bananas','samples': [17, 2, 67]},
    {'id': 3,'type': 'pears','samples': [91, 22, 34]}
  ]
];

我想使用lodash将上述数组简化并合并为一个数组,以便将样本连接在一起,如下所示:

I want to reduce and merge the above array using lodash into one so that the samples get concatenated together like so:

fruitSamples = [
  {'id': 1, 'type': 'apples', 'samples': [1,2,3,5,2,9,11,2,33]},
  {'id': 2, 'type': 'bananas', 'samples': [1,2,7,1,7,7,17,2,67]},
  {'id': 3, 'type': 'pears', 'samples': [1,2,3,12,21,32,91,22,34]},
]

我尝试了许多不同的方法,但是由于我想以最短的途径解决此问题,您的建议是什么?

I have tried many different approaches but since I want the shortest possible way of solving this what would be your recommendations?

我已经尝试过了:

let test = _(fruitSamples)
  .flatten()
  .groupBy('type')
  .map(_.spread(_.merge))
  .value();

console.log(test);

这给了我以下结果,该结果没有将样本连接起来:

This gives me the following result, which does not concatenate the samples:

test = [
  {'id': 1,'type': 'apples','samples': [11, 2, 33]},
  {'id': 2,'type': 'bananas','samples': [17, 2, 67]},
  {'id': 3,'type': 'pears','samples': [91, 22, 34]}
]

我觉得使用_.mergeWith可能是正确的答案,如果是这样,我不确定如何实现以最佳方式寻求实现mergeWith的帮助.有什么建议吗?

I feel using _.mergeWith might be the right answer, if so, I am looking for help implementing mergeWith in the best possible way as I am not sure how to do it. Any suggestions?

推荐答案

而不是_.merge,您可以尝试使用

Instead of _.merge, you could try using _.mergeWith that accepts a customizer function which you can use to customize the assigned values.

来自官方文档:

此方法类似于_.merge,不同之处在于它接受定制程序,即 调用以生成目标和源的合并值 特性.

This method is like _.merge except that it accepts customizer which is invoked to produce the merged values of the destination and source properties.

let fruitSamples = [
  [
    {'id': 1,'type': 'apples','samples': [1, 2, 3]},
    {'id': 2,'type': 'bananas','samples': [1, 2, 7]},
    {'id': 3,'type': 'pears','samples': [1, 2, 3]}
  ],
  [
    {'id': 1,'type': 'apples','samples': [5, 2, 9]},
    {'id': 2,'type': 'bananas','samples': [1, 7, 7]},
    {'id': 3,'type': 'pears','samples': [12, 21, 32]}
  ],
  [
    {'id': 1,'type': 'apples','samples': [11, 2, 33]},
    {'id': 2,'type': 'bananas','samples': [17, 2, 67]},
    {'id': 3,'type': 'pears','samples': [91, 22, 34]}
  ]
];

function customizer(objValue, srcValue) {
  if (_.isArray(objValue)) {
    return objValue.concat(srcValue);
  }
}

let test = _(fruitSamples)
  .flatten()
  .groupBy('type')
  .map(_.spread((...values) => {
    return _.mergeWith(...values, customizer);
  }))
  .value();
  
console.log(test);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

这篇关于如何使用lodash在对象数组中合并/连接相同对象属性的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆