'UnionBy using ID and update_date greater than

I`m using lodash and I need a help with unify two arrays using ID but update the second array only if update_date is greater than other object.

Example:

First Array:

[
 {
  id:xpto
  name:test
  update_date:1519211809934 //Because I`m using Date.now()
 },
{
  id:testes
  name:nameTest
  update_date:1519211809934 //Because I`m using Date.now()
 }
]

Second Array:

[
 {
  id:xpto
  name:test
  update_date:1519211809934 //Because I`m using Date.now()
 }
]

I know, if I using _unionBy(array1, array2, id) the first replace the second, but, how I can add one condition to unify only if update_date is greater than the other object?

Thanks for your help!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source