通过相等属性合并对象,并在同一数组中通过不同属性合并对象

gcfabri

我得到了这个数组,并且想要在代码,名称,文档和值的值都相等并且将月份和小时连接在一起时连接对象。我尝试使用lodash失败。

有没有办法使用lodash做到这一点?我尝试将groupBy与unionBy一起使用没有成功..结果是混乱的数组。谢谢。

    const arrayOfObject = [
    {
      code: '6666',
      name: 'Foo Bar',
      doc: '60170150166',
      value: '158.56',
      month: '082011',
      hours: '20'
    },
    {
      code: '6666',
      name: 'Foo Bar',
      doc: '60170150166',
      value: '158.56',
      month: '092011',
      hours: '20'
    },
    {
      code: '6666',
      name: 'Foo Bar',
      doc: '60170150166',
      value: '158.56',
      month: '102011',
      hours: '10'
    },
    {
      code: '6666',
      name: 'Foo Bar',
      doc: '60170150166',
      value: '169.81',
      month: '042012',
      hours: '10'
    },
    {
      code: '6666',
      name: 'Foo Bar',
      doc: '60170150166',
      value: '169.81',
      month: '052012',
      hours: '10'
    }
  ];

这是预期的结果:

const expectedArray = [
    {
      code: '6666',
      name: 'Foo Bar',
      doc: '60170150166',
      value: '158.56',
      month: '082011, 092011, 102011',
      hours: '20, 20, 10'
    },
    {
      code: '6666',
      name: 'Foo Bar',
      doc: '60170150166',
      value: '169.81',
      month: '042012, 052012',
      hours: '10, 10'
    }
  ];
或Drori

使用键对键进行分组_.groupBy(),然后使用_.map()合并每个组的项目_.mergeWith()

const combine = _.flow([
  arr => _.groupBy(arr, o => [o.code, o.name, o.document, o.value].join('-')),
  groups => _.map(groups, g => _.mergeWith(...g, (o, s, k) => {
    if(k === 'month' || k === 'hours') return `${o}, ${s}`;
  }))
]);

 const arrayOfObject = [{"code":"6666","name":"Foo Bar","doc":"60170150166","value":"158.56","month":"082011","hours":"20"},{"code":"6666","name":"Foo Bar","doc":"60170150166","value":"158.56","month":"092011","hours":"20"},{"code":"6666","name":"Foo Bar","doc":"60170150166","value":"158.56","month":"102011","hours":"10"},{"code":"6666","name":"Foo Bar","doc":"60170150166","value":"169.81","month":"042012","hours":"10"},{"code":"6666","name":"Foo Bar","doc":"60170150166","value":"169.81","month":"052012","hours":"10"}];
  
const result = combine(arrayOfObject);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

合并对象数组的相同属性,并在该对象数组中为不同属性创建对象数组

通过两个具有不同键的键合并同一数组中的对象

通过属性键合并javascript对象数组

PHP通过同一数组中的值合并

通过通用属性对对象进行分组,将其余属性合并为数组中的单独对象

如何通过 JS 中的三个不同属性对嵌套对象数组进行分组

计算对象数组中的不同属性

合并数组中的对象属性

Python 3:合并具有不同属性的对象列表

从对象数组中的对象属性合并数组

通过通用键对对象数组进行分组,将嵌套属性合并到数组中

如何将同一属性的数组和对象“合并/合并”到平面数组中(使用Javascript)?

合并数组中的对象并保持相同属性的最大值

如何将具有相同属性的对象合并到数组中?

如何将具有相同属性的对象合并到单个数组中?

AngularJS:通过匹配不同的属性获取数组中的对象属性?

根据Javascript中的公共值合并同一数组中的对象

通过覆盖 compareTo() 方法根据对象的不同属性对数组进行排序

如何在共享相同未知密钥的同一数组中合并多个对象?

在单个键上合并同一数组中的对象

根据属性合并数组中的对象

电源外壳。合并数组中对象的属性

如何基于属性合并数组中的对象

在 Parallel.Invoke 中更新同一对象的不同属性是线程安全的吗?

将具有相同属性的两个对象的方法合并到每个属性的数组中

如果对象的属性值在两个对象数组中相等,则将这些对象合并为单个对象

通过对象属性从数组中删除对象

如何通过对象数组中的键值合并或合并数据?

通过对象属性获取数组中的不同元素