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

时间:2018-12-14 14:28:13

标签: javascript lodash

我得到这个数组,并且想要在代码,名称,文档和值的值都相等并且将月份和小时连接在一起时连接对象。我尝试使用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'
    }
  ];

3 个答案:

答案 0 :(得分:1)

使用_.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>

答案 1 :(得分:1)

使用原生Array#reduce()创建组对象,然后使用Object.values()返回结果数组。

对我来说,这更容易阅读,然后结合了多种lodash方法

请注意,我将hours和month`更改为数组,因为通常它比string更有用,...如有必要,进行调整

const grouped = Object.values(
    arrayOfObject.reduce((a,c)=>{
       const key = [c.code, c.name, c.doc, c.value].join('|');
       a[key] = a[key] || Object.assign({}, c, {month:[],hours:[]});
       a[key].month.push(c.month)
       a[key].hours.push(c.hours);
       return a
    },{})
);

console.log(grouped)
<script>
  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'
    }
  ];
</script>

答案 2 :(得分:0)

只需使用_.unionBy

let groupedObject = _.unionBy(arrayOfObject, (element) => {
  return (element.code && element.name && element.doc && element.value)
})

有效的提琴:https://jsfiddle.net/oqtLpbxr/

还有T.J.评论中提到了Crowder,如果您至少自己尝试一下并发布代码,那就太好了。