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

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

王林
王林Original
2020-11-26 11:36:372085browse

Jquery method to determine whether an element is in an array: You can use the [$.inArray("Element (string)", array name)] method to determine. If the element exists in the array, then this method Will return the index of the element in the array.

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

The operating environment of this tutorial: windows10 system, jquery2.2.4 version, suitable for all brands of computers.

Principle:

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

(Learning video sharing: jquery video tutorial)

Introduction to judgment method:

You can use the method provided by Jquery: $.inArray("Element (string)", array name) makes a judgment. When the element (string) exists, the subscript of the element in the array is returned. If it does not exist, -1 is returned.

Code implementation:

$(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 an element 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