Home > Web Front-end > JS Tutorial > How to determine whether an element is an array element in jquery

How to determine whether an element is an array element in jquery

coldplay.xixi
Release: 2023-01-04 09:37:46
Original
2771 people have browsed it

Jquery method to determine whether an element is an array element: Use [$.inArray("Element (string)", array name)] to determine. When the element exists, return the element in the array. Subscript, returns [-1] if it does not exist.

How to determine whether an element is an array element in jquery

The operating environment of this tutorial: windows7 system, jquery3.2.1 version, Dell G3 computer.

Recommended: jquery video tutorial

Jquery method to determine whether it is an array element:

To determine whether the array contains In principle, a certain element is to traverse the entire array and then determine whether it is equal.

You can use the method provided by Jquery:

$.inArray("Element(String )", array name) Make a judgment. When the element (string) exists, return the subscript of the element in the array. If it does not exist, return -1

 $(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 free learning Recommended: javascript learning tutorial

The above is the detailed content of How to determine whether an element is an array element 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