Angular & Typescript - How to get unique items or remove duplicates from array?

1 minute read

In Typescript, there is no built-in function to retrieve the unique items from Array. The below code is a custom function that uses contains (in typescript includes) to check the new array and pushes the items to it.

Code:

RemoveDuplicates(ary:any[], column:string):any[]
{
    var aryUniqueItems = [];

    ary.forEach(item => {
        if(!aryUniqueItems.includes(item[column])){
            aryUniqueItems.push(item[column]);
        }
    });

    return aryUniqueItems;
}

Example: Input Array:

1     User 1     India
2     User 1     USA
3     User 1     UK
4     User 2     Japan
5     User 2     China
6     User 3     Finland

Output:

User 1
User 2
User 3

Leave a comment