Home  >  Article  >  Backend Development  >  in_array() in javascript finds element value in array

in_array() in javascript finds element value in array

巴扎黑
巴扎黑Original
2016-11-24 14:39:441349browse

in_array() has the same function as in_array() in php

Js code

//Find element value in array

function in_array(v, a) {  
  var i;  
  for (i = 0; i < a.length; i++) {  
    if (v === a[i]) {  
      return i;  
    }  
  }  
  return -1;  
} // 返回-1表示没找到,返回其他值表示找到的索


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