How to remove duplicates from an array
There are a number of ways to remove duplicates from an array, most of them require you to iterate through all elements once or twice. Here is a small code snippet to filter duplicate values from an array.
array.filter((v, i, a) => a.findIndex(t => t === v) === i)
If you have array elements as objects, still you can check duplicates by extending this a little bit.
For example, let's say the array we have as below.
[{ id: 1, value: 1 }, { id: 2, value : 2}]
You can still remove duplicates using the following method.
array.filter((v, i, a) => a.findIndex(t => t.value === v.value) === i)
© Heshan Wanigasooriya.RSS🍪 This site does not track you.