remove duplicates from array angular stackblitz Archives - Tech Insights Unveiling Tomorrow's Tech Today, Where Innovation Meets Insight Thu, 14 Sep 2023 11:43:13 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.2 https://i0.wp.com/reactconf.org/wp-content/uploads/2023/11/cropped-reactconf.png?fit=32%2C32&ssl=1 remove duplicates from array angular stackblitz Archives - Tech Insights 32 32 230003556 How to remove duplicates from an Array in TypeScript https://reactconf.org/how-to-remove-duplicates-from-an-array-in-typescript/ https://reactconf.org/how-to-remove-duplicates-from-an-array-in-typescript/#respond Thu, 14 Sep 2023 11:43:13 +0000 https://labpys.com/?p=1682 In this article, we will learn how to remove duplicates from an Array in TypeScript. Set Method The simplest and most efficient way to remove duplicates from an array is …

The post How to remove duplicates from an Array in TypeScript appeared first on Tech Insights.

]]>
In this article, we will learn how to remove duplicates from an Array in TypeScript.

Set Method

The simplest and most efficient way to remove duplicates from an array is using Set. A Set is a built-in function in a JavaScript object that stores unique values.

const setArray = new Set([1001,1020,1400,2300,1002]);
// Array.from Method

const Uniquevalue= Array.from(new Set(setArray));

// Spread Method
const Uniquevalue = [... new Set(setArray)];

console.log(Uniquevalue);

Output:

[1001, 1020, 1400, 2300, 1002]

Filter() Method

Using Filter with the ‘IndexOf’ array method, we can remove duplicates from the array object.

const Uniquevalue = [1001,1020,1400,2300,1002,6500,5001,4001,5001];
 
console.log('Filter Method'); 
const uniqueEmpcode = Uniquevalue.filter((value,index,items)=> items.indexOf(value)===index);
console.log(Uniquevalue);

Output:

[1001, 1020, 1400, 2300, 1002, 6500, 5001, 4001] 

reduce() Method

With the ‘reduce()’ method we can also remove the duplicate elements in the array.

console.log('Reduce Method');
const Uniquevalue = [1001,1020,1400,2300,1002,6500,5001,4001,5001];
 
const uniqueEmpcodereduce = Uniquevalue.reduce(
    (value,items)=> value.includes(items) ? value : [...value,items],       
    []);
console.log(uniqueEmpcodereduce);

Here is the Output:

[1001, 1020, 1400, 2300, 1002, 6500, 5001, 4001] 

More Article Convert Set into an Array in TypeScript

The post How to remove duplicates from an Array in TypeScript appeared first on Tech Insights.

]]>
https://reactconf.org/how-to-remove-duplicates-from-an-array-in-typescript/feed/ 0 1682