Home  >  Article  >  Web Front-end  >  How to determine whether a specified value exists in an array in javascript

How to determine whether a specified value exists in an array in javascript

PHPz
PHPzOriginal
2023-04-24 10:51:47766browse

JavaScript is a very powerful programming language that supports a variety of data types and data structures, including arrays. When writing JavaScript applications, we often need to write code to check whether a specific value exists in an array. At this point, we need to use some specific technologies and methods to achieve this goal.

This article will introduce how to determine whether a value exists in an array in JavaScript. We will explore some of the built-in methods in JavaScript as well as ways to implement custom functions. If you are currently developing a JavaScript-based application and need to perform this task in it, then this article will be helpful to you.

Built-in methods in JavaScript

JavaScript provides some built-in methods to determine whether a value exists in an array. These methods include indexOf() and includes() methods, let’s see how they work.

indexOf() method

The indexOf() method is used to find a specific value in the array, and if it exists, returns the index position of the value. If the value does not exist, -1 is returned. The following is sample code:

let arr = [10, 20, 30, 40, 50];

console.log(arr.indexOf(30)); // 2
console.log(arr.indexOf(60)); // -1

includes() method

includes() method is used to find a specific value in the array and returns true if it exists. If the value does not exist, returns false. The following is the sample code:

let arr = [10, 20, 30, 40, 50];

console.log(arr.includes(30)); // true
console.log(arr.includes(60)); // false

Custom function

In addition to the built-in methods, we can also implement custom functions to determine whether a value exists in the array. Here is a sample code with a custom function:

function contains(arr, val) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === val) {
      return true;
    }
  }

  return false;
}

let arr = [10, 20, 30, 40, 50];

console.log(contains(arr, 30)); // true
console.log(contains(arr, 60)); // false

This function will iterate through each element in the array and check if it is equal to the value we are looking for. Returns true if the value is found, false otherwise.

Note that we can achieve a more flexible search by passing a function as the second parameter. For example, we could write a function that checks whether an element in an array is greater than 10, and then finds an element greater than 10.

function contains(arr, fn) {
  for (let i = 0; i < arr.length; i++) {
    if (fn(arr[i])) {
      return true;
    }
  }

  return false;
}

let arr = [10, 20, 30, 40, 50];

console.log(contains(arr, (val) => val > 10)); // true
console.log(contains(arr, (val) => val > 60)); // false

This function accepts an array and a function as parameters. This function will be used to check each element in the array and return a Boolean value indicating whether the element matches. In our example, we pass a function that checks if the element is greater than 10.

Summary

This article introduces how to determine whether a value exists in an array in JavaScript. We explored some of the built-in methods in JavaScript as well as ways to implement custom functions. Which method you choose to use depends on your specific needs and preferences. No matter which method you choose, I hope this article will be helpful.

The above is the detailed content of How to determine whether a specified value 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