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)".
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)
Parameter | Description |
---|---|
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("给定元素是不存在的"); }
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])
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("给定元素是不存在的"); }
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)
Parameter | Description |
---|---|
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 |
thisValue | Optional. 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("给定元素是不存在的"); }
The output result is:
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)
Parameters | Description |
---|---|
item | must. 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("给定元素是不存在的"); }
方法5:利用数组的lastIndexOf()方法
lastIndexOf()方法可搜索数组中的元素,并返回它最后出现的位置。如果要检索的元素没有出现,则该方法返回 -1。
array.lastIndexOf(item,start)
参数 | 描述 |
---|---|
item | 必需。规定需检索的字符串值。 |
start | 可选的整数参数。规定在字符串中开始检索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略该参数,则将从字符串的最后一个字符处开始检索。 |
实现思想:利用该方法检查指定值在数组的最后出现位置,如果位置是存在的,则包含该给定元素;如果返回-1,则不包含该给定元素。
下面给出实现代码::
var fruits = ['苹果',"香蕉", '榴莲', '橘子', '菠萝蜜',"梨子"]; var b = fruits.lastIndexOf("葡萄"); if (b>0) { console.log("给定元素是存在的"); } else { console.log("给定元素是不存在的"); }
输出结果为:
【相关推荐: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!