I have an array like this:
array = ["123", "456", "#123"]
I want to find the element which contains the substring "#"
. I tried array.includes("#")
and array.indexOf("#")
but it didn't work.
How can I check if any of the strings in this array contain the substring "#"
?
To check if a Python string contains the desired substring, you can use the "in" operator or the string. find() method. To get the first index of a substring in a string, you can use the string. index().
JavaScript Array includes() The includes() method returns true if an array contains a specified value. The includes() method returns false if the value is not found. The includes() method is case sensitive.
Use the includes() Method to Check if a String Is Present in a TypeScript Array. The includes() method determines whether a target element is present in the array. It takes in the target element and returns a boolean value to represent the value in the array.
The includes() method returns true if a string contains a specified string. Otherwise it returns false . The includes() method is case sensitive.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With