<html>
<head>
<title>an array contains a object using javascript</title>
</head>
<body style="background:lightgray">
<p>Click the button to test:</p>
<button onclick="test()">clear array</button>
</br>
<input id="item" type="text" value="a"/>
</br>
<textarea id="edit" style="height:200px;width:240px">
</textarea>
<script>
var arr=["a","b","c"];
function test() {
var val=document.getElementById("item").value;
var text="";
if(contains(arr,val)){
text="["+arr+"] contains "+val;
}else{
text="["+arr+"] not contains "+val;
}
document.getElementById("edit").value+='\n'+text;
}
function contains(arr,item) {
return arr.indexOf(item)>-1;
}
document.getElementById("edit").value="["+arr+"]";
</script>
</body>
</html>
Reference:
http://stackoverflow.com/questions/237104/array-containsobj-in-javascript
Comments
Post a Comment