Home  >  Article  >  Web Front-end  >  How to determine if an array contains specified elements in jquery

How to determine if an array contains specified elements in jquery

coldplay.xixi
coldplay.xixiOriginal
2020-11-25 15:04:472183browse

The jquery method to determine whether the array contains the specified element: first traverse the entire array; then determine whether they are equal. The code is [var arr = [ "xml", "html", "css", "js" ]; $.inArray("js", arr);】.

How to determine if an array contains specified elements in jquery

The operating environment of this tutorial: Windows 7 system, jquery version 3.2.1. This method is suitable for all brands of computers.

Jquery method to determine whether an array contains a specified element:

To determine whether an array contains a certain element, in principle, it is to traverse the entire array, and then To determine whether they are equal, the

code is as follows:

var arr = [ "xml", "html", "css", "js" ];
$.inArray("js", arr);  //返回 3,

If it is not included in the array, it returns -1;

Related free learning recommendations: javascript (video)

The above is the detailed content of How to determine if an array contains specified elements 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