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')