我尝试使用include()函数,但对我来说返回false,如下所示:
<script>
var fruits = ["Banana-orange", "Orange", "Apple", "Mango"];
console.log(fruits.includes("-")); // returnes false, should be true
</script>
Run Code Online (Sandbox Code Playgroud)
如您所见,“香蕉橙色”包含连字符。我希望结果返回true。
我使用了错误的功能吗?
在Array#includes与(在你的情况完全字符串匹配)的数组元素进行比较。要使其工作,请将数组中的值与空白(使用Array#join)连接,并使用String#includes搜索子字符串的方法。
var fruits = ["Banana-orange", "Orange", "Apple", "Mango"];
console.log(fruits.join('').includes("-")); Run Code Online (Sandbox Code Playgroud)
Array#some迭代并检查至少一个包含使用String#includes方法的字符串。
var fruits = ["Banana-orange", "Orange", "Apple", "Mango"];
console.log(fruits.some(v => v.includes("-")));Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
140 次 |
| 最近记录: |