5

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 ?

Yasser Moussa
  • 2,209
  • 6
  • 26
  • 39
  • 2
    possible duplicate of [array.contains(obj) in JavaScript](http://stackoverflow.com/questions/237104/array-containsobj-in-javascript) – Felix Kling Jan 22 '13 at 15:04

2 Answers2

22

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.

VisioN
  • 143,310
  • 32
  • 282
  • 281
4
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
JohnJohnGa
  • 15,446
  • 19
  • 62
  • 87