K20GH
K20GH

Reputation: 6271

Remove word from string if included in array?

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?

Upvotes: 0

Views: 93

Answers (5)

saidutt
saidutt

Reputation: 289

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);

Upvotes: 0

Huỳnh Tuân
Huỳnh Tuân

Reputation: 116

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, "");
    } 
});

Upvotes: 0

saidutt
saidutt

Reputation: 289

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, "");
});

Upvotes: -1

ziggy wiggy
ziggy wiggy

Reputation: 1067

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.

Upvotes: 0

Code Maniac
Code Maniac

Reputation: 37755

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)

Upvotes: 2

Related Questions