Handling Array Uniqueness with Complex Objects in Lodash
I've tried everything I can think of but I'm currently working on a project using Lodash in JavaScript, and I'm working with an scenario with filtering an array of objects to maintain uniqueness based on a specific key. I have an array of user objects, and I want to ensure that I only keep unique entries based on the `email` property. Here's what my data looks like: ```javascript const users = [ { id: 1, email: 'alice@example.com', name: 'Alice' }, { id: 2, email: 'bob@example.com', name: 'Bob' }, { id: 3, email: 'alice@example.com', name: 'Alice Smith' }, { id: 4, email: 'charlie@example.com', name: 'Charlie' } ]; ``` I've tried using Lodash's `_.uniqBy` method, but it seems to return only the first occurrence of each unique item. My goal is to filter out duplicates while retaining the last occurrence of each unique email. Here's the attempt I made: ```javascript const uniqueUsers = _.uniqBy(users, 'email'); // This keeps the first occurrence ``` However, I need to modify this so that it keeps the last occurrence instead. I've looked through the Lodash documentation but couldn't find a straightforward way to achieve this. I also considered using `Array.prototype.reduce`, but I'm not sure how to implement that effectively. Could someone suggest a solution or best practice for handling this scenario? I'm using Lodash version 4.17.21, and my project runs on Node.js 14.x. Any help would be greatly appreciated! The project is a web app built with Javascript. Any feedback is welcome!