-2

Is there a better way of checking if a word exists in an array of strings without including Punctuation Marks?

what I have tried,

const sArray=['Lorem','Ipsum','typesetting-','industry.','Ipsum?','has' ]


console.log(sArray.toString().replaceAll(".","").includes("industry")) //true

console.log(sArray.includes("industry")) //false
2
  • 4
    sArray.some(e => e.includes("industry")). Pay attention to the fact that the includes here is String.prototype.includes, not Array.prototype.includes. Commented Oct 10, 2022 at 10:25
  • As @GerardoFurtado said or, in case you need to manipulate the string when it is present, you can use Array.prototype.find() (const foundItem = sArray.find(e => e.includes("industry")); if (foundItem !== -1) { /* manipulate the string */ })
    – secan
    Commented Oct 10, 2022 at 10:40

2 Answers 2

0

Something like this?

sArray.some(str => str.includes('industry'))
0

If you do a lot of operations on the array, I suggest creating a new reference of the array after replacing, then dealing with the new array.

const sArray=['Lorem','Ipsum','typesetting-','industry.','Ipsum?','has' ]

const trasformedArray = sArray.map(i => i.replaceAll('.', ''));

console.log(trasformedArray.includes("industry"))

Not the answer you're looking for? Browse other questions tagged or ask your own question.