我有array = ["Michael","Abraham", "Gabriel","Festus", "Ahab"]
如何返回包含 myValue = "ab"
的数组中的元素应该是(Abraham,Gabriel,Ahab),我理解“indexOf”检查完整的匹配。
最佳答案
您可以使用数组的过滤方法来完成此操作,请引用下面的代码片段:
const array = ["Michael","Abigail","Gabriel","Festus", "Ahab"]
console.log(array.filter((name) => name.indexOf("ab") > -1));
关于javascript - 检查数组中是否包含某个值并返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55500961/