Home Web Front-end JS Tutorial How to determine if an element is in an array in javascript

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

Nov 24, 2021 pm 05:02 PM
javascript array

Method: 1. Use the indexOf() function to get the position where the specified element first appears in the array. If the return value is "-1", the element is not in the array; 2. Use lastIndexOf() to get the specified element. The position of the last occurrence of the element in the array. If the return value is "-1" the element is not in the array.

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

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In javascript, you can use the indexOf() and lastIndexOf() functions to determine whether the element is in the array. The

indexOf() and lastIndexOf() methods can retrieve array elements and return the index position of the specified element; if the specified element does not exist, "-1" is returned.

Use indexOf() to find elements in the array

indexOf() returns the first match of an element value in the array The index, or -1 if the specified value is not found. The usage is as follows:

array.indexOf(item,start)
Copy after login
  • item Required. The element to find.

  • #start Optional integer parameter. Specifies the position in the array to start searching. Its legal values ​​are 0 to stringObject.length - 1. If this parameter is omitted, the search will start from the first character of the string. The

#indexOf() method performs a search in ascending index order, that is, retrieval from left to right. When retrieving, the array elements will be compared congruently with the searchElement parameter value ===.

Example: Find whether an element is in an array

var arr = ["ab","cd","ef","ab","cd"];
var str="cd";
if(arr.indexOf(str)===-1){
	console.log("指定元素:"+str+" 不在数组中");
}else{
	console.log("指定元素: "+str+" 在数组中");
}
Copy after login

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

##Modify the value you need to find:

var str="gh";
if(arr.indexOf(str)===-1){
	console.log("指定元素:"+str+" 不在数组中");
}else{
	console.log("指定元素: "+str+" 在数组中");
}
Copy after login

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

Use lastIndexOf() to find elements in the array

indexOf() returns the last 1 of an element value in the array The index of a match, or -1 if the specified value is not found. Its usage is the same as indexOf().

Example: Find whether an element is in an array

var arr = ["ab","cd","ef","ab","cd"];
var str="gx";
if(arr.lastIndexOf(str)===-1){
	console.log("指定元素:"+str+" 不在数组中");
}else{
	console.log("指定元素: "+str+" 在数组中");
}
Copy after login

How to determine if an element is in an array in javascript##[Related recommendations:

javascript learning tutorial

The above is the detailed content of How to determine if an element is in an array in javascript. For more information, please follow other related articles on the PHP Chinese website!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to remove duplicate elements from PHP array using foreach loop? How to remove duplicate elements from PHP array using foreach loop? Apr 27, 2024 am 11:33 AM

How to remove duplicate elements from PHP array using foreach loop?

PHP array key value flipping: Comparative performance analysis of different methods PHP array key value flipping: Comparative performance analysis of different methods May 03, 2024 pm 09:03 PM

PHP array key value flipping: Comparative performance analysis of different methods

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy May 01, 2024 pm 12:30 PM

The Art of PHP Array Deep Copy: Using Different Methods to Achieve a Perfect Copy

PHP array multi-dimensional sorting practice: from simple to complex scenarios PHP array multi-dimensional sorting practice: from simple to complex scenarios Apr 29, 2024 pm 09:12 PM

PHP array multi-dimensional sorting practice: from simple to complex scenarios

Application of PHP array grouping function in data sorting Application of PHP array grouping function in data sorting May 04, 2024 pm 01:03 PM

Application of PHP array grouping function in data sorting

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods Apr 30, 2024 pm 03:42 PM

Best Practices for Deep Copying PHP Arrays: Discover Efficient Methods

The role of PHP array grouping function in finding duplicate elements The role of PHP array grouping function in finding duplicate elements May 05, 2024 am 09:21 AM

The role of PHP array grouping function in finding duplicate elements

PHP array merging and deduplication algorithm: parallel solution PHP array merging and deduplication algorithm: parallel solution Apr 18, 2024 pm 02:30 PM

PHP array merging and deduplication algorithm: parallel solution

See all articles