var arr = new Array(1,2,3,2,5); var p = arr.indexOf(3) //p = 2 p = arr.indexOf(7) //p = -1 p = arr.indexOf(2) //p = 1 if (arr.indexOf(5)) alert("true") else alert("flase");
var arr = new Array(1,2,3,2,5,11,14); var p = arr.lastIndexOf(3) //p = 2 p = arr.lastIndexOf(7) //p = -1 p = arr.lastIndexOf(2) //p = 3
var a=["1","b","gg","ed"]; if ( a.includes("b") ) alert("exist");
var a=[1,2,3,4]; if ( 2 in a ) alert("exist");
var zy = new Array(); zy["a3"]="aaa";zy["b3"]="bbb";zy["c3"]="ccc"; var str = "a3"; if (str in zy){ alert(zy[str]); }
Use
Array.prototype.contains = function(elem) { for (var i in this) { if (this[i] == elem) return true; } return false; } var arr = [4,16,9]; if (arr.contains(4)) alert("yes"); else alert("no");