I've been trying to remove duplicate words from a string, and it's not working.
I have the current string:
const categories = 'mexican, restaurant, mexican, food, restaurant'
and I want this outcome:
const x = 'mexican restaurant food'
I have tried the following:
const x = categories.replace(/,/g, '');
const uniqueList = x
.split()
.filter((currentItem, i, allItems) => {
return i === allItems.indexOf(currentItem);
})
.join();
Which is giving me:
uniqueList = 'chinese restaurant chinese food restaurant'
What is wrong with the code above?
Set. This will automatically give you a unique list that you can convert back to a string.splitwith no argument just gives you back a singleton array holding the entire string. You should split on the space character.