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

我不明白为什么我的代码无法正常工作。

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

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


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

最佳答案

.toUpperCase()只能用于字符串,不能用于您正在执行的整个数组。使用map和capitalise数组中的字符串,然后使用

.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/

10-13 08:57
查看更多