Home >Web Front-end >JS Tutorial >Use js to determine whether an array contains an element (similar to in_array() in php)_javascript skills

Use js to determine whether an array contains an element (similar to in_array() in php)_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 17:09:271101browse

while case is the fastest

Copy code The code is as follows:

function contains(arr, str) {
var i = arr.length;
while (i--) {
if (arr[i] === str) {
return true;
} }
}
return false;
}
Statement:
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