'Confused on github copilot solution that works but not sure why

So I was stuck on a bug in my React App (I am new to react and JS in general) where I wanted to filter a list of company names so that they are all unique and dont repeat. So having access to github copilot I turned it on and out came a solution that worked but I have no idea why or what its doing. If anyone could help me?

    useEffect(() => {
        const companyNames = data.map(app => app.companyName);
        const uniqueCompanies = [...new Set(companyNames)];
        setCompanies(uniqueCompanies);
    },[data]);

I dont understand how [...new Set(companyNames)] filters out the unique names or what is happening. To my understanding you can only use the spread operator on an array that already exists so didn't know where the new was coming from either.



Solution 1:[1]

The expression const uniqueCompanies = [...new Set(companyNames)]; contains the following interesting properties:

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LauriM