0

If I have an array (category) that have multiple of the same values (retail for example)

How do I remove duplicates and just return just one (Retail, Technology, Auto etc)

So it just leaves data like this:

Before:

category: “Retail, Retail, Retail, Technology”,

After:

category: “Retail, Technology”

I have a code example here:

https://codesandbox.io/s/late-cdn-2stdj


Update:

See when I do it with const of categories it works:

https://codesandbox.io/s/inspiring-sky-zx4mo

but when I try and do it with a .map to access the array it doesn't that is where I am at:

https://codesandbox.io/s/late-cdn-2stdj

Tired_Man
  • 97
  • 1
  • 15

1 Answers1

1

Use Set:

const categories = "Retail, Retail, Retail, Technology";

const listCategories = [...new Set(categories.split(/,\s*/))];

Or alternative:
const listCategories = [...new Set(categories.match(/[A-z]+/g))];


const result = listCategories.join(", ");
console.log({result})
Chau Giang
  • 1,414
  • 11
  • 19