Merge Array of Objects by Property using Lodash

后端 未结 7 1716
北恋
北恋 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: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);
    

提交回复
热议问题