检查数组javascript中是否有字符串 [英] Check if string inside an array javascript
本文介绍了检查数组javascript中是否有字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个日期名称
的数组,我想检查一下如果 sunday
- 首字母资本或者小 - 在这个数组中最好的做法是什么?
If i had an array of days names
and i wanted to check for example if sunday
- first letter capital or small - in this array what would be the best thing to do ?
推荐答案
您也可以使用 Array.indexOf
:
You may also use Array.indexOf
:
var days = ["monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
"sunday"];
function isInArray(days, day) {
return days.indexOf(day.toLowerCase()) > -1;
}
isInArray(days, "Sunday"); // true
isInArray(days, "sunday"); // true
isInArray(days, "sUnDaY"); // true
isInArray(days, "Anyday"); // false
检查 MDN 。
这篇关于检查数组javascript中是否有字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文