使用lodash或纯js把数组对象中相同的字段的值合并

1. lodash解决办法

1.1 数组内对象型数据
let fruitSamples = [
      { id: 1, type: 'apples', samples: [{ id: 1, name: 1 }] },
      { id: 2, type: 'bananas', samples: [{ id: 2, name: 2 }] },
      { id: 3, type: 'pears', samples: [{ id: 3, name: 3 }] },
      { id: 1, type: 'apples', samples: [{ id: 11, name: 11 }] },
    ]

    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, 'test')
结果:

[
  { id: 1, type: 'apples', samples: [{ id: 1, name: 1 },{ id: 11, name: 11 }] },
  { id: 2, type: 'bananas', samples: [id: 2, name: 2 }] },
  { id: 3, type: 'pears', samples: [{ id: 3, name: 3 }] }
]
1.2 数组内数组内对象型数据
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);
结果:

[
  { 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] }
]

注:无论samples是单纯的数组内string或者数组内number还是数组内Object都支持

2.1 纯js处理
let fruitSamples = [
    {
        foo: "A",
        bar: [
            { baz: "1", qux: "a" },
            { baz: "2", qux: "b" }
        ]
    },
    {
        foo: "B",
        bar: [
            { baz: "3", qux: "c" },
            { baz: "4", qux: "d" }
        ]
    },
    {
        foo: "A",
        bar: [
            { baz: "5", qux: "e" },
            { baz: "6", qux: "f" }
        ]
    },
    {
        foo: "B",
        bar: [
            { baz: "7", qux: "g" },
            { baz: "8", qux: "h" }
        ]
    }
]
const hash = Object.create(null)
const test = fruitSamples.filter(function (o) {
  if (!hash[o.foo]) {
     hash[o.foo] = o.bar
     return true
  }
  Array.prototype.push.apply(hash[o.foo], o.bar)
})
console.log(test)
结果:

[
    {
        foo: "A",
        bar: [
            { baz: "1", qux: "a" },
            { baz: "2", qux: "b" },
            { baz: "5", qux: "e" },
            { baz: "6", qux: "f" }
        ]
    },
    {
        foo: "B",
        bar: [
            { baz: "3", qux: "c" },
            { baz: "4", qux: "d" },
            { baz: "7", qux: "g" },
            { baz: "8", qux: "h" }
        ]
    }
]

小伙伴们择优使用

推荐阅读更多精彩内容