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

青灯夜游
Release: 2021-06-29 16:40:15
Original
3041 people have browsed it

In JS, you can use the some() method to determine whether there is a specified element in the array. This method can detect whether there is an element in the array that meets the specified conditions. If it exists, it will return true, if it does not exist, it will return false; syntax format "array.some(callbackfn[, thisArg]);".

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

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

JS some() method: detect whether there are elements that meet the conditions in the array

JavaScript some() method Used to detect whether there is an element in the array that meets the specified conditions. If it exists, it returns true, if it does not exist, it returns false. Thinking about it from another angle, some() can also be used to detect whether all elements in the array do not meet the specified conditions. If they do not meet the specified conditions, it will return false. If one or more elements match, it will return true.

some() The specific usage is as follows:

array.some(callbackfn[, thisArg]);
Copy after login

Parameter description:

  • array: required parameter, an array object.

  • callbackfn: required parameter, a function that receives up to three parameters. The some() method calls the callbackfn function for each element in the array until callbackfn returns true, or until the end of the array is reached.

  • thisArg: Optional parameter, an object that can refer to the this keyword in the callbackfn function. If thisArg is omitted, undefined will be used to return false.

The song() method calls the callbackfn function on each array element in ascending index order until the callbackfn function returns true. The some() method returns true immediately if an element is found that causes callbackfn to return true. If the callback does not return true for any element, the some() method returns false.

The some() method does not call this callback function for missing elements in the array. In addition to array objects, the some() method can be used by any object that has a length property and has a numerically indexed property name, such as associative arrays, Arguments, and so on.

Example:

Detect whether the value of an element in an array is an odd number. If the some() method detects an even number, it returns true and prompts that not all are odd; if no even number is detected, it prompts that all are odd.

function f(value, index, ar) { if (value % 2 == 0) { return true; } } var a = [1,15,4,10,11,22]; var evens = a.some(f); if (evens) { console.log("不全是奇数。"); } else { console.log("全是奇数。"); }
Copy after login

【Related recommendations:javascript learning tutorial

The above is the detailed content of How to determine 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!