JavaScript 判断字符串或数字数组内是否存在某个字符串或者是某个数字

存在则返回 >-1 的数值,即索引位置

# 字符串类型判断是否存在
testdata = "1"
let testString = ["1", "2", "3", "4", "5", "6"];
  if(test.indexOf(testdata) != -1){
    
    
  console.log("字符串数组 testString 内存在字符串 "1" ")
  }


# 数字类型判断是否存在
testindex = 3
let testIndex = [1, 2, 3, 4, 5, 6];
  if(test.indexOf(testindex) != -1){
    
    
  		console.log("数字数组 testIndex  内存在数字 1")
  }
  

猜你喜欢

转载自blog.csdn.net/qq_42701659/article/details/132695233