Home > Web Front-end > JS Tutorial > body text

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

coldplay.xixi
Release: 2023-01-05 16:08:40
Original
3747 people have browsed it

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 ] )
Copy after login
  • 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)
Copy after login

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!

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