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 ?
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
Check the browser compatibility in MDN.
function is_in_array(s,your_array) {
for (var i = 0; i < your_array.length; i++) {
if (your_array[i].toLowerCase() === s.toLowerCase()) return true;
}
return false;
}
Usage:
var arr = ["hello","ToTo"];
is_in_array("toto",arr) //true
is_in_array("todto",arr) //false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With