0

A relatively straight forward issue I think, however I'm having a few issues getting it right.

I've got an array of strings and a sentence. If any of the words in the array appear in the sentence, remove them.

const products = ["premium t-shirt", "t-shirt", "sweatshirt", "baseball tee", "v-neck t-shirt", "long sleeve t-shirt", "raglan baseball tee", "pullover hoodie", "tank top", "zip hoodie",
"hoodie", "shirt", "tee", "tshirt"];

const name = "I'd Rather Be Beekeeping Save The Bees Honey Bee Beehive Zip Hoodie T-Shirt".toLowerCase();
let strippedName;
products.forEach(p => {
    if(name.includes(p)) strippedName = name.replace(p, "");
});

The above removes the word shirt, but not t-shirt. Theres also the issue of it needed to loop around on the strippedName variable rather tan the name.

I'm not sure if theres a better way to do this though?

3
  • It might be caused by capital letters of T-Shirt compared to the t-shirt in the array? Commented Oct 29, 2019 at 16:41
  • @WilsonSim, name is being made to lowercase .toLowerCase() Commented Oct 29, 2019 at 16:42
  • My mistake, did not see that appended at the end. Commented Oct 29, 2019 at 16:45

5 Answers 5

2

You're override strippedName everyti,e with latest replaced value from name string, whereas you need to pass the use last replaced value when using the replace on next value

const products = ["premium t-shirt", "t-shirt", "sweatshirt", "baseball tee", "v-neck t-shirt", "long sleeve t-shirt", "raglan baseball tee", "pullover hoodie", "tank top", "zip hoodie","hoodie", "shirt", "tee", "tshirt"];
const name = "I'd Rather Be Beekeeping Save The Bees Honey Bee Beehive Zip Hoodie T-Shirt".toLowerCase();
let strippedName = name;

products.forEach(p => {
  if (name.includes(p)){
   strippedName = strippedName.replace(p, "");
  }
});

console.log(strippedName)

Sign up to request clarification or add additional context in comments.

2 Comments

The order of the items in products will have an impact on its success. For example, if the sentence contains tshirt, the t will currently be left.
@ziggywiggy yes totally agree with you, not only order but also the submatching words will also be replaced as OP is using regex in original code without any word boundaries so i assume OP isn't much bothered about that so i kept the answer focused on what i see actual problem in code
0

Convert the array to a case insensitive, global regex.

const products = ["premium t-shirt", "t-shirt", "sweatshirt", "baseball tee", "v-neck t-shirt", "long sleeve t-shirt", "raglan baseball tee", "pullover hoodie", "tank top", "zip hoodie",
  "hoodie", "shirt", "tee", "tshirt"
];

const name = "I'd Rather Be Beekeeping Save The Bees Honey Bee Beehive Zip Hoodie T-Shirt";

let strippedName = name.replace(new RegExp(products.join("|"), "gi"), "");

console.log(strippedName);
If any of the products characters are regex special characters, they'll first need to be escaped.

Comments

0

I think you mean:

const products = ["premium t-shirt", "t-shirt", "sweatshirt", "baseball tee", "v-neck t-shirt", "long sleeve t-shirt", "raglan baseball tee", "pullover hoodie", "tank top", "zip hoodie",
"hoodie", "shirt", "tee", "tshirt"];

var name = "I'd Rather Be Beekeeping Save The Bees Honey Bee Beehive Zip Hoodie T-Shirt".toLowerCase();
let strippedName = name;
products.forEach(p => {
    if(name.includes(p)){
        strippedName = strippedName.replace(p, "");
    } 
});

Comments

0
const products = ["premium t-shirt", "t-shirt", "sweatshirt", "baseball tee", "v-neck t-shirt", "long sleeve t-shirt", "raglan baseball tee", "pullover hoodie", "tank top", "zip hoodie",
"hoodie", "shirt", "tee", "tshirt"];

const name = "I'd Rather Be Beekeeping Save The Bees Honey Bee Beehive Zip Hoodie T-Shirt".toLowerCase();
let strippedName = name;
products.forEach(p => {
  if(strippedName.includes(p)) {
    strippedName = strippedName.replace(p, "");
  }
});
console.log(strippedName);

Comments

-1

The includes method is case sensitive. It will consider 'T-shirt' and 't-shirt' differently. You should consider transforming the string to a upper/lower case and then perform your include method.

const products = ["premium t-shirt", "t-shirt", "sweatshirt", "baseball tee", "v-neck t-shirt", "long sleeve t-shirt", "raglan baseball tee", "pullover hoodie", "tank top", "zip hoodie", "hoodie", "shirt", "tee", "tshirt"];

const name = "I'd Rather Be Beekeeping Save The Bees Honey Bee Beehive Zip Hoodie T-Shirt".
const n = name.toUpperCase();
let strippedName;
products.forEach(p => {
    if(n.includes(p.toUpperCase())) strippedName = new.replace(p, "");
});

5 Comments

The OP is already doing that and since the array values are lower-case, you should use .toLowerCase().
My bad, I see that now. But you should also be changing the case in your if block for individual array elements
Not needed because all array items are already lower case and the .toLowerCase() call on name returns a new string that is all lower-case. That string is being captured as name and name is what the condition is testing.
Also, it's a bad idea to name anything new.
The real problem is that the OP is checking name upon each loop iteration, but setting the replaced string into strippedName.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.