Basic string and array operations questions

I have a list of keywords that my boss wants me to target in google ads. The list he gave me has many repeat elements but each element seems to be separated by a carriage return. I want to remove all duplicate elements and return an array of unique elements. This is the work I did so far:
Codepen Link
I thought it was working and I organized the elements alphabetically to double check my work and behold:
Term “BICYCLE MANUFACTURER” appears twice in the the resulting array.
Why is it returning two copies and not working as I would expect it to?

The spaces between the words are not the same, the second one is using a Non-breaking space (char code 160)

Try replacing all whitespace characters with a single space (char code 32) to normalize the different spacing methods. As a side note, you can use the Set Object to easily filter your list down to only unique entries without having to resort to JQuery or extra loops.

function createGoogleAds(str){
  let result = str.toUpperCase().split(/\n/);
  // Clean str of odd spacing, ending ':', and '1-' 
  result = result.map(el => el.replace(/\s+/g, ' ')
                              .replace(/:$|1-/g, '') // remove this if these are desired
                              .trim())
                 .filter(el => el.length > 0);
  result = new Set(result); // Keeps only unique values
  return [...result].sort();  // Sort and return unique values
};

Your code has some Gremlins…

I see but i still do not understand why your answer explains why its still not working, yes I agree with you that some of them have the   character
however, I used .trim() method so I thought that this would be a non-issue? Please explain.

Because you are only trimming the start and end of the full string inside your map.


Just for fun, I tried a version using normalize.

function createGoogleAds(str) {
  const strings = str.toUpperCase().split(/\n/);

  return [
    ...new Set(
      strings
        .map(str => str.trim().normalize('NFKC'))
        .filter(str => str != '')
        .sort()
    )
  ];
}

It seems to work, but I would definitely go with regex. It does not clean the string of any unwanted characters or extra spaces because this version is kind of pointless and you should not use it anyway so I didn’t go any further with it.

BTW. You also have strings like this that are a little harder to deal with (missing parentheses).

Vocational training school (electrician, plumbing, carpentry, metal worker, auto repair
Vocational training school (electrician, plumbing, carpentry, metal worker, auto repair)