Home  >  Article  >  Web Front-end  >  How to determine if a value is in an array in jquery

How to determine if a value is in an array in jquery

王林
王林Original
2020-11-24 10:49:144526browse

Jquery method to determine whether the value is in the array: You can use the [$.inArray("Element (string)", array name)] method to determine. When the element exists, return the element in the array If the subscript in does not exist, [-1] will be returned.

How to determine if a value is in an array in jquery

The operating environment of this tutorial: windows10 system, jquery3.2.1 version. This method is suitable for all brands of computers.

(Learning video sharing: javascript video tutorial)

Jquery method to determine whether a value is in an array:

Thinking :

Traverse the entire array and then determine whether it is equal.

Methods provided by Jquery:

$.inArray("元素(字符串)",数组名称)

When the element (string) exists, return the subscript of the element in the array, and return -1 when it does not exist.

$(function () {
          var array = ["asp.net", "asp.net mvc", "html5", "css3", "jquery", "JavaScript"];
             var index = $.inArray("asp.net mvc", array);   //结果:index=1
            if (index >= 0) {
                console.log("数组array包含asp.net mvc下标为:" + index);
           } else {
                console.log("数组array 不包含asp.net mvc下标为:" + index);
           }
      });

Related recommendations: js tutorial

The above is the detailed content of How to determine if a value is in an array in jquery. For more information, please follow other related articles on the PHP Chinese website!

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