In my current project, I am facing the challenge of looping through an array of objects, each containing an email property. The task at hand is to traverse the data (in CSV format) and remove all records except for the last one if they share the same email address. This final record holds the value that needs to be updated in the end. Despite multiple attempts using .find method, I have been unable to achieve the desired result. It seems like there may be some missing piece in my logic.
let arr = [
{name: 'stefano', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9cefe8f9fadcf1fdf5f0b2fff3f1">[email protected]</a>'},
{name: 'steve', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8bbadbbb398b5b9b1b4f6bbb7b5">[email protected]</a>'},
{name: 'weave', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed9e99888bad808c8481c38e8280">[email protected]</a>'},
{name: 'peave', email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ec80998f87ac818d8580c28f8381">[email protected]</a>'}
];
let keepLast = arr.find( (obj,idx) => {
let found = 0;
if(obj.email === '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afdcdbcac9efc2cec6c381ccc0c2">[email protected]</a>') {
++found;
}
if(found > 1) {
// Somehow remove the previous iteration catch on match
}
});
The current status of my progress involves a need to find a way to retain the index of the last matching record so that it can be easily removed once another match is encountered.