Mel Sy Gallosa Mel Sy Gallosa - 9 months ago 26
Javascript Question

Finding certain strings in array (Javascript)

I'm having a problem in searching in an array. I wanted to check if a certain string
exists in one of the elements, example


["4_1", "4_2", "4_3"]

I will check if the string "4" exists in one of the variables.

Thank you!

The The

The Easiest way is to use Array.prototype.join && indexOf methods

["4_1", "4_2", "4_3"].join("").indexOf("4")


According to @t.niese's comment this answer may cause wrong result, for example if you are looking for 14 it will return 2 - what is wrong because your array doesnt contain element which begins on 14. In this case better to use @Nina's answer