Home > Web Front-end > JS Tutorial > body text

js uses for loop to query whether a certain value exists in an array_javascript skills

WBOY
Release: 2016-05-16 16:39:49
Original
1719 people have browsed it

It should be because IE8 does not support indexOf, so write a for loop method to determine whether it exists and return true false

function isCon(arr, val){ 
for(var i=0; i<arr.length; i++){ 
if(arr[i] == val) 
return true; 
} 
return false; 
}
Copy after login

Add this method in js and call the method at the calling location

window.onload = function(){ 
var arr=new Array(1,2,3,2,4,2,5,6,7,2,4,3,18,12,41,87); 
alert(isCon(arr, 8)); 
}
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template