Home > Article > Web Front-end > How to find whether a specified element exists in an array in JavaScript
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.
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!