Merge Array of Objects by Property using Lodash

后端 未结 7 1677
北恋
北恋 2020-12-01 06:25

I have two arrays of objects that represent email addresses that have a label and a value:

var original = [
  {
    label: \'private\',
    value: \'private@         


        
7条回答
  •  温柔的废话
    2020-12-01 06:44

    Perhaps a bit late, but all the solutions I have seen don't join both arrays correctly, they use one of the arrays to loop on and any excess elements in the second array don't get added (assuming this is what is required).

    I had the same observation so put something together myself. This is working for my use case, which is to merge each object if the value of the 'label' field matches:

    const dataSetHashes = dataSets.map(dataSet => _.keyBy(dataSet, 'label'))
    const resultHash = _.merge(
      {},
      ...dataSetLookups
    )
    const result = Object.values(resultLookup)
    

提交回复
热议问题