Remove duplicate objects with certain properties

Let's assume that you have the following object:

javascript
let tasks = {
    
    task_1:{
        name: 'Erikmartinjordan',
        duty: 'Writing article',
        timeStampEnd: 1575875952440,
        timeStampIni: 1654654678788            
    },
    task_2:{
        name: 'Tiempone',
        duty: 'Deploying new component',
        timeStampEnd: 1475875952440,
        timeStampIni: 1875875357873            
    },
    task_3: {
        name: 'Erikmartinjordan',
        duty: 'Writing article',
        timeStampEnd: 1175875952440,
        timeStampIni: 1275875357873
    }
    
}

If you want to remove duplicate objects with certain properties, you could use the filter() function.

For example, let's assume that you want to filter tasks with equal name and duty:

javascript
const duplicateTasks = (current, index, array) => {
    
    // Finding tasks with equal name and duty properties
    let duplicate = array.findIndex(task => task.name === current.name && task.duty === current.duty);
    
    // As you want to only display the first element
    // If duplicate === index -> First element found
    // If duplicate !== index -> Not the first element
    return duplicate === index;
    
}

let filtered = Object.values(tasks).filter(duplicateTasks).map(task => task);

console.log(filtered);
/*
[
    {
        name: 'Erikmartinjordan',
        duty: 'Writing article',
        timeStampEnd: 1575875952440,
        timeStampIni: 1654654678788            
    },
    {
        name: 'Tiempone',
        duty: 'Deploying new component',
        timeStampEnd: 1475875952440,
        timeStampIni: 1875875357873            
    }
]
*/

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.