array name stays and it duplicates and repeating this process just clogs the list up.
Thank you.
setListItems(contents.data);
console.log(contents.data);
array name stays and it duplicates and repeating this process just clogs the list up.
Thank you.
setListItems(contents.data);
console.log(contents.data);
Taken straight from MSDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#remove_duplicate_elements_from_the_array
// Use to remove duplicate elements from the array
const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]
console.log([...new Set(numbers)])
// [2, 3, 4, 5, 6, 7, 32]
To convert the array contents.data
to Set, do this:
const setData = new Set(contents.data);
That will remove all the duplicate items. Then to convert it back, do this:
const uniqueArray = Array.from(setData);
The above will only work if the original array (contents.data
) consisted of primitive values. If it was an array of objects then this will not work as-is and will require some changes.