Merge Array of Objects by Property using Lodash

后端 未结 7 1572
北恋
北恋 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:39

    I know it is not what asked for but just in case someone stumbled up on this page here is how you do this in ramda:

    var original = [
      { label: 'private', value: 'private@johndoe.com' },
      { label: 'work', value: 'work@johndoe.com' }
    ];
    
    var updated = [
      { label: 'private', value: 'me@johndoe.com' },
      { label: 'school', value: 'schol@johndoe.com' }
    ];
    
    unionWith(eqBy(prop('label')), updated, original);
    
    0 讨论(0)
  • 2020-12-01 06:43

    In case you are using lodash 3.x where _.unionBy() was not there, you can combine _.union() and _.uniq() to get the same result.

    var original = [
      { label: 'private', value: 'private@johndoe.com' },
      { label: 'work', value: 'work@johndoe.com' }
    ];
    
    var update = [
      { label: 'private', value: 'me@johndoe.com' },
      { label: 'school', value: 'schol@johndoe.com' }
    ];
    
    var result = _.uniq(_.union(update, original), "label"); 
    
    console.log(result);
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-01 06:45

    Convert the lists to objects keyed by label, merge them by _.assign, and convert it back to an array. It will even retain order of the items on most browsers.

    var original = [
      {
        label: 'private',
        value: 'private@johndoe.com'
      },
      {
        label: 'work',
        value: 'work@johndoe.com'
      }
    ];
    
    var update = [
      {
        label: 'private',
        value: 'me@johndoe.com'
      },
      {
        label: 'school',
        value: 'schol@johndoe.com'
      }
    ];
    
    console.log(
      _.map(
        _.assign(
          _.mapKeys(original, v => v.label),
          _.mapKeys(update, v => v.label)
        )
      )
    );
    
    
    // or remove more duplicated code using spread
    
    console.log(
      _.map(
        _.assign(
          ...[original, update].map(
            coll => _.mapKeys(coll, v => v.label)
          )
        )
      )
    );
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.js"></script>

    0 讨论(0)
  • 2020-12-01 06:46

    Here is another way to merge two objects using Lodash:

    let a = [{
        content: 'aaa',
        name: 'bbb2'
      },
      {
        content: 'aad',
        name: 'ccd'
      }
    ];
    
    let b = [{
        content: 'aaa',
        name: 'bbb'
      },
      {
        content: 'aad1',
        name: 'ccd1'
      }
    ];
    
    let c = [...a, ...b];
    
    let d = _.uniq(c, function(data) {
      return data.content;
    })
    
    console.log(d);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.9.1/underscore-min.js"></script>

    0 讨论(0)
  • 2020-12-01 06:48

    _.unionBy():
    This method is like _.union except that it accepts iteratee which is invoked for each element of each arrays to generate the criterion by which uniqueness is computed. Result values are chosen from the first array in which the value occurs.

    var original = [
      { label: 'private', value: 'private@johndoe.com' },
      { label: 'work', value: 'work@johndoe.com' }
    ];
    
    var update = [
      { label: 'private', value: 'me@johndoe.com' },
      { label: 'school', value: 'schol@johndoe.com' }
    ];
    
    var result = _.unionBy(update, original, "label");
    
    console.log(result);
    <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>

    0 讨论(0)
提交回复
热议问题