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

作者 : 开心源码 本文共2114个字,预计阅读时间需要6分钟 发布时间: 2022-05-14 共155人阅读

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

小伙伴们择优使用

说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 使用lodash或者纯js把数组对象中相同的字段的值合并

发表回复