javascript - 结合使用 .toUpperCase() 和 .includes()

标签 javascript case-sensitive

我正在尝试使用 .includes() 方法搜索数组。由于区分大小写,我不希望使用 .includes() 方法无法检测到确实位于数组中的元素。因此,我尝试将 .toUpperCase() 与 .includes() 结合使用

我不明白为什么我的代码不起作用。

var euroTour = ["France", "the Netherlands", "the UK", "Spain",
                "Portugal"];

var findCountry = euroTour.toUpperCase().includes('FRANCE');

我希望使用上面的代码,将 true 写入文档。然而,什么也没写。当我完全删除 .toUpperCase() 方法时,如预期的那样, false 被写入文档,因为搜索的主题“FRANCE”与数组的实际元素“France”不同。

最佳答案

.toUpperCase() 只能用于字符串,不能用于我们正在做的整个数组。使用映射并大写数组中的字符串,然后使用

进行检查
.includes

var euroTour = ["France", "the Netherlands", "the UK", "Spain",
                "Portugal"];

var findCountry = euroTour.map((e)=>e.toUpperCase()).includes('FRANCE');
console.log(findCountry)

关于javascript - 结合使用 .toUpperCase() 和 .includes(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54247558/

相关文章:

Excel 区分大小写的 COUNTIF 带通配符 : native function?

nhibernate - 在 FluentNhibernate 上禁用表名双引号

javascript - Node mustache 渲染服务器端

javascript - 如何用JavaScript清除网站的缓存?

javascript - 使用 jQuery Mobile 表单,我应该将验证消息放在哪里?

java - 不区分大小写的字符串数组比较

jquery - 通过查询字符串突出显示菜单忽略大小写

javascript - 禁用指定列的 angularjs 数据表排序

javascript - Firefox - 显示来自扩展程序/插件的网络通知

Java:Jlist setSelectedValue() 但 ignoreCase