Home  >  Article  >  php教程  >  Use js to determine whether an array contains an element (similar to in_array() in php)

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

高洛峰
高洛峰Original
2016-12-22 13:39:131701browse

while case is the fastest

function contains(arr, str) {
    var i = arr.length;
    while (i--) {
           if (arr[i] === str) {
           return true;
           }   
    }   
    return false;
}


For more information on using js to determine whether an array contains a certain element (similar to in_array() in php), please pay attention to the PHP Chinese website for related articles!


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