0

I am using lodash 3.10 and i need to unite 2 arrays.

[
  {
    "name": "DESIGEMPRESA",
    "value": "CMIP"
  },
  {
    "name": "DSP_DIRECAO",
    "value": "CMIP@400@1900-01-01"
  },
  {
    "name": "MES",
    "value": "12"
  }
]

and this one that are prety much the same :

    [
      {
        "name": "DESIGEMPRESA",
        "value": "CMIP"
      },
      {
       name: "CUSTO", 
       value: ">100"
      }
    ]

I know theres is a simple way to do it as merge doesnt work in this cas. Apreciate your help Thanks.

Leonel Matias Domingos
  • 1,922
  • 5
  • 29
  • 53
  • https://stackoverflow.com/questions/31740155/lodash-remove-duplicates-from-array – Elad Jan 14 '18 at 07:53
  • 1
    Possible duplicate of [Unique array of objects and merge duplications using lodash or underscore](https://stackoverflow.com/questions/43877957/unique-array-of-objects-and-merge-duplications-using-lodash-or-underscore) – Elad Jan 14 '18 at 07:54
  • Define "unite". Often the best way to communicate what you want in these cases is to complete your example. What should be the result? – Eric Simonton Jan 15 '18 at 13:57

1 Answers1

1

var array1 = [
  {
    "name": "DESIGEMPRESA",
    "value": "CMIP"
  },
  {
    "name": "DSP_DIRECAO",
    "value": "CMIP@400@1900-01-01"
  },
  {
    "name": "MES",
    "value": "12"
  }
];
var array2 =  [
      {
        "name": "DESIGEMPRESA",
        "value": "CMIP"
      },
      {
       "name": "CUSTO", 
       "value": ">100"
      }
    ];
    
 var merged = _(array1)
  .concat(array2)
  .groupBy("name")
  .map(_.spread(_.merge))
  .value();

console.log(merged);
 
   <script src="https://cdn.jsdelivr.net/lodash/4.13.1/lodash.min.js"></script>
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396