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

Determine the value existing in the array

php中世界最好的语言
Release: 2018-06-08 10:45:14
Original
1263 people have browsed it

This time I will bring you the judgment of the value that exists in the array, and what are the precautions for judging the value that exists in the array. The following is a practical case, let's take a look.

<!DOCTYPE>
<html>
<head>
<title>jquery判断值是否存在于数组中</title>
<meta charset="utf-8">
</head>
<body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
//参数第一个为数组,第二个为需要判断是否存在于数组中的值
function isInArray(arr,val){
  var str = ","+arr.join(",")+",";
  if(str.indexOf(","+val+",") != "-1"){
    //该数存在于数组中
    arr.splice($.inArray(val,arr),1);  //把该数从数组中删除
    console.log(arr);
  }else{
    //该数不存在于数组中
    console.log("不存在");
  }
}
var arr = [1,28,60,80,6];
isInArray(arr,28);//存在,删除元素28
isInArray(arr,18);//不存在,提示“不存在”
</script>
</body>
</html>
Copy after login

Run results:

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Operation Angularjs cross-domain setting whitelist

How to use Angular data binding mechanism

The above is the detailed content of Determine the value existing in the array. For more information, please follow other related articles on the PHP Chinese website!

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