检查字符串数组是否包含子字符串 [英] Check if an array of strings contains a substring
本文介绍了检查字符串数组是否包含子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的数组:
array = ["123", "456", "#123"]
我想找到包含子字符串 "#"
的元素.我试过 array.includes("#")
和 array.indexOf("#")
但没有用.
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 "#"
?
推荐答案
因为包含会将 '#' 与每个数组元素进行比较.
Because includes will compare '#' with each array element.
让我们试试 some 或 查找 如果要查找是否要准确获取元素
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)
这篇关于检查字符串数组是否包含子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文