-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
Rickhomes
  • 135
  • 1
  • 8
  • 4
    `sArray.some(e => e.includes("industry"))`. Pay attention to the fact that the `includes` here is `String.prototype.includes`, not `Array.prototype.includes`. – Gerardo Furtado Oct 10 '22 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 Oct 10 '22 at 10:40

2 Answers2

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"))
Mina
  • 14,386
  • 3
  • 13
  • 26