Skip to content

Instantly share code, notes, and snippets.

@kpunith8
Last active July 14, 2021 08:02
Show Gist options
  • Save kpunith8/55ebcd3e5e10cb0c03d5165cfd3276a8 to your computer and use it in GitHub Desktop.
Save kpunith8/55ebcd3e5e10cb0c03d5165cfd3276a8 to your computer and use it in GitHub Desktop.
Keep only latest updated docs using lodash and keep unique entries
const lastUpdatedEngines =
_.map(
_.filter(
_.groupBy(actualData, 'engine'), // Group each engine
status => _.size(status) > 1 // Look for arrays having more than one entry
),
engines => {
const lastUpdatedEngine = engines.reduce((a, b) =>
new Date(a?.updatedAt) > new Date(b?.updatedAt) ? a : b // compare the updatedAt for each engine and pick the latest
)
return lastUpdatedEngine
}
)
const uniqueUpdatedEngines = _.unionBy(lastUpdatedEngines, actualData, 'engine')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment