How can I somehow split/separate my JavaScript variable by comma (,).
And then check if value-of-any-of-the-separated-strings = "something"
For example, my variable has the value 1,2,3,4,5,6,7,8,9,10,2212312, and I want to check if any of the numbers are = 7 in a IF-Statement.
Does anyone have any ideas how this can be done?
First, split the string by
",". Then, useindexOfon the split-string array to see if the target string is found (-1means it wasn’t found in the array). For example:References:
String.prototype.split– https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/splitArray.prototype.indexOf– https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf