Home Web Front-end Front-end Q&A How to find whether an item exists in es6

How to find whether an item exists in es6

Jan 03, 2023 pm 03:24 PM
es6 array

Method: 1. Use find(); 2. Use includes(), the syntax is "array.includes(value)"; 3. Use some(), the syntax is "function f(v) {if (v === value) {return true;}}var b= array.some(f);”; 4. Use indexOf(), the syntax is “array.indexOf(value)”; 5. Use lastIndexOf(), the syntax is “array .lastIndexOf(value)".

How to find whether an item exists in es6

The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.

Method 1: Using the find() method of the array

The find() method is used to find the first element in the array that meets the conditions , if there is no element that meets the conditions, undefined

find() will be returned. For an empty array, the function will not be executed.

find() does not change the original value of the array.

array.find(function(currentValue, index, arr),thisValue)
Copy after login
ParameterDescription
function(currentValue, index,arr) Required. The function that needs to be executed for each element of the array.
Function parameters: Parameter description currentValue is required. The current element index is optional. The index value arr of the current element is optional. The array object to which the current element belongs
thisValue is optional. The value passed to the function usually uses the "this" value.
If this parameter is empty, "undefined" will be passed to the "this" value

Return value:

  • Return The first array element value that meets the test conditions. If there is no element that meets the conditions, undefined is returned.

Example: Determine whether the fruits array contains the given element "Durian"

function f(value) {
        return value === '橘子';
}
	
var fruits = ['苹果',"香蕉", '榴莲', '橘子', '菠萝蜜',"梨子"];

if(fruits.find(f)!=undefined){
	console.log("给定元素是存在的");
}else{
	console.log("给定元素是不存在的");
}
Copy after login

How to find whether an item exists in es6

Method 2 : Use the includes() method of the array

The includes() method is used to determine whether an array contains a specified value. If so, it returns true, otherwise false.

arr.includes(searchElement [, fromIndex])
Copy after login
  • searchElement Required. The element value to be found.

  • fromIndex Optional. Start looking for searchElement at this index. If negative, the search starts at the index of array.length fromIndex in ascending order. Default is 0.

Example: Determine whether the fruits array contains the given element "Durian"

var fruits = ['苹果',"香蕉", '榴莲', '橘子', '菠萝蜜',"梨子"];
if(fruits.includes('榴莲')){
	console.log("给定元素是存在的");
}else{
	console.log("给定元素是不存在的");
}
Copy after login

How to find whether an item exists in es6

Method 3: Use the some() method of the array

The some() method can detect whether there are elements that meet the conditions in the array. Thinking about it from another angle, it 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.

array.every(function callbackfn(Value,index,array),thisValue)
Copy after login
ParameterDescription
function(currentValue, index,arr) must. Function, each element in the array will execute this function
Function parameters:
Parameter description currentValue must be. The value index of the current element is optional. The index value arr of the current element is optional. The array object to which the current element belongs
thisValueOptional. The object is used as the execution callback, passed to the function, and used as the value of "this".
If thisValue is omitted, the value of "this" is "undefined"

Return value: Boolean value. Returns true if any element in the array satisfies the condition, otherwise returns false.

Example:

function f(value, index, ar) {
    if (value === '橘子') {
        return true;
    }
}
var fruits = ['苹果',"香蕉", '榴莲', '橘子', '菠萝蜜',"梨子"];
var b = fruits.some(f);
if (b) {
   console.log("给定元素是存在的");
} else {
   console.log("给定元素是不存在的");
}
Copy after login

The output result is:

How to find whether an item exists in es6

Method 4: Utilization The indexOf() method of array

indexOf() method can return the first occurrence position of a specified element in the array. If the element to be retrieved does not appear, the method returns -1.

array.indexOf(item,start)
Copy after login
ParametersDescription
itemmust. 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.

实现思想:利用该方法检查指定值在数组的第一次出现位置,如果位置是存在的,则包含该给定元素。如果返回-1,则不包含该给定元素。

下面给出实现代码:

var fruits = ['苹果',"香蕉", '榴莲', '橘子', '菠萝蜜',"梨子"];
var b = fruits.indexOf("桃子");
if (b>0) {
   console.log("给定元素是存在的");
} else {
   console.log("给定元素是不存在的");
}
Copy after login

方法5:利用数组的lastIndexOf()方法

lastIndexOf()方法可搜索数组中的元素,并返回它最后出现的位置。如果要检索的元素没有出现,则该方法返回 -1。

array.lastIndexOf(item,start)
Copy after login
参数描述
item必需。规定需检索的字符串值。
start可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的最后一个字符处开始检索。

实现思想:利用该方法检查指定值在数组的最后出现位置,如果位置是存在的,则包含该给定元素;如果返回-1,则不包含该给定元素。

下面给出实现代码::

var fruits = ['苹果',"香蕉", '榴莲', '橘子', '菠萝蜜',"梨子"];
var b = fruits.lastIndexOf("葡萄");
if (b>0) {
   console.log("给定元素是存在的");
} else {
   console.log("给定元素是不存在的");
}
Copy after login

输出结果为:

How to find whether an item exists in es6

【相关推荐:javascript视频教程编程视频

The above is the detailed content of How to find whether an item exists in es6. 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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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 reverse an array in ES6 How to reverse an array in ES6 Oct 26, 2022 pm 06:19 PM

In ES6, you can use the reverse() method of the array object to achieve array reversal. This method is used to reverse the order of the elements in the array, putting the last element first and the first element last. The syntax "array.reverse()". The reverse() method will modify the original array. If you do not want to modify it, you need to use it with the expansion operator "...", and the syntax is "[...array].reverse()".

How to find different items in two arrays in es6 How to find different items in two arrays in es6 Nov 01, 2022 pm 06:07 PM

Steps: 1. Convert the two arrays to set types respectively, with the syntax "newA=new Set(a);newB=new Set(b);"; 2. Use has() and filter() to find the difference set, with the syntax " new Set([...newA].filter(x =>!newB.has(x)))", the difference set elements will be included in a set collection and returned; 3. Use Array.from to convert the set into an array Type, syntax "Array.from(collection)".

How to delete elements in an array in es6 How to delete elements in an array in es6 May 19, 2022 pm 07:17 PM

Deletion method: 1. Use shift() to delete the first element, the syntax is "arr.shift()"; 2. Use pop() to delete the last element, the syntax is "arr.pop()"; 3. Use splice() Delete any element based on the subscript, the syntax is "arr.splice(start subscript, delete the number of elements)".

How to delete the first element from es6 array How to delete the first element from es6 array May 19, 2022 pm 06:35 PM

3 deletion methods: 1. Use shift(), the syntax is "array object.shift()". 2. Use splice() to delete an element with a starting index of 0. The syntax is "array object.splice(0,1)". 3. Use delete to delete the array element with index 0, the syntax is "delete array name[0]".

Are arrays some and every es6 methods? Are arrays some and every es6 methods? May 19, 2022 pm 05:40 PM

some and every are es6 methods. some() and every() are new methods of the es6 Array object. some() is used to detect whether there are elements that meet the conditions in the array. As long as one of them matches, it will return true; and every() is used to detect all elements of the array. Whether the elements all meet the specified conditions.

How to convert es6 array into string How to convert es6 array into string Oct 18, 2022 pm 02:48 PM

3 conversion methods: 1. Use the join() function to put all elements in the array into a string, the syntax is "array object.join (separator)"; 2. Use the toString() function, the syntax is "array object.toString" ()" or "array object.toString().replace(/,/gi,'separator')"; 3. Use the toLocaleString() function, the syntax is "array object.toLocaleString()".

How to convert array-like object to array in es6 How to convert array-like object to array in es6 Jan 03, 2023 pm 02:33 PM

Conversion method: 1. Use the "for in" statement to convert the array-like object into an array, the syntax is "for(var i in obj){console.log(arr.push(obj[i]));}"; 2. Use Built-in object keys and values, the syntax is "Object.keys(obj)" and "Object.values(obj)"; 3. Use the from() function of the Array object, the syntax is "Array.from(obj)".

How to sum two arrays in ES6 How to sum two arrays in ES6 May 19, 2022 pm 06:14 PM

Summing method: 1. Use concat() to merge two arrays. The syntax "array1.concat(array2)" will add the elements of array 2 to the end of array 1; 2. Use reduce() to calculate the value of the merged array. And, the syntax is "array.reduce(function(p,c){sum=p+c;})".

See all articles