使用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" }
        ]
    }
]

小夥伴們擇優使用

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章