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

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

王林
Release: 2020-11-30 14:25:51
Original
4625 people have browsed it

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("元素(字符串)",数组名称)
Copy after login

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);
           }
      });
Copy after login

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!

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