Check if an array of strings contains a substring in javascript

user7334203 picture user7334203 · Jun 8, 2017 · Viewed 27.9k times · Source

i'm a little bit confused with this task I have an array like this: array = ["123", "456", "#123"] i want to find the element which contains the #. I used array.includes("#") and array.indexOf("#") but it didn't work Any ideas??

Answer

Tai Le picture Tai Le · Jun 8, 2017

Because includes will compare '#' with each array element.

Let's try with some or find if you want to find if you want to get exactly element

var array = ["123", "456", "#123"];

var el = array.find(a =>a.includes("#"));

console.log(el)