Learn how to remove the duplicate values in the array
Simple Way is using Set (which only contains unique value).
var array = [1,2,3,4,1,2,3];
var unique = [... new Set(array)] ;
//or
var unique = Array.from (new Set(array) );
Another way is
var elements = [1,2,3,4,1,2,3];
var uniqueArray = elements.filter(function(element, currentIndex) {
return elements.indexOf(element) == currentIndex;
});
The filter() method creates a new array with all elements that pass the test.
The indexOf() method returns the first index at which a given element can be found.
Consider we have an array elements = [1,2,3,1]
test case
element = 1, index = 0
elements.indexOf(1) == 0 // true
element = 2, index = 1
elements.indexOf(2) == 1// true
element = 3, index = 2
elements.indexOf(3) == 2// true
element = 1, index = 3
elements.indexOf(1) == 3// false because the indexOf 1 is 0 so this test is failed , 1 is rejected π ββοΈ .
You can read about set in details here.
If you find this helpful surprise π me here.
Share if you feel happy π π πΒ .
Follow Javascript Jeepπ if you feel worthy.