Home  >  Article  >  Web Front-end  >  How to find whether a specified element exists in an array in JavaScript

How to find whether a specified element exists in an array in JavaScript

coldplay.xixi
coldplay.xixiOriginal
2021-04-15 14:06:463712browse

JavaScript method to find whether a specified element exists in an array: 1. Use jQuery, the code is [jQuery.inArray( value, array [, fromIndex] )]; 2. Use the [indexOf()] method to return a certain a specified string value.

How to find whether a specified element exists in an array in JavaScript

The operating environment of this tutorial: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

JavaScript method to find whether a specified element exists in an array:

1.jQuery

   jQuery.inArray( value, array [, fromIndex ] )
  • value

    Type: Anything

    The value to find.

  • array

    Type: Array

    An array through which to search.

  • fromIndex

    Type: Number

    Array index value, indicating where to start searching. The default value is 0, which will search the entire array.

  • $.inArray() method is similar to JavaScript's native .indexOf() method. It returns -1 when no matching element is found. If the first element of the array matches value (parameter), then $.inArray() returns 0.

2.indexof

The indexOf() method returns the position where a specified string value first appears in the string.

Grammar

 stringObject.indexOf(searchvalue,fromindex)

Related free learning recommendations: javascript video tutorial

The above is the detailed content of How to find whether a specified element exists in an array in JavaScript. 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