how to get distinct elements in array react code example
Example 1: javascript get unique values from array
const myArray = [1,2,3,1,5,8,1,2,9,4];
const unique = [...new Set(myArray)];
const myString = ["a","b","c","a","d","b"];
const uniqueString = [...new Set(myString)];
Example 2: javascript get distinct values from array
const categories = ['General', 'Exotic', 'Extreme', 'Extreme', 'General' ,'Water', 'Extreme']
.filter((value, index, categoryArray) => categoryArray.indexOf(value) === index);
This will return an array that has the unique category names
['General', 'Exotic', 'Extreme', 'Water']