es6 The some() method is used to detect whether there are elements in the array that meet the specified conditions. It returns true if it exists, and false if it does not exist. From another angle, it can also be used to detect whether all elements in the array are If the specified conditions are not met, false is returned if none are met, and true is returned if one or more are met.
The operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
The some() method can be used to detect whether the elements in the array meet the specified conditions (provided by the function). It returns true if it exists, and returns false if it does not exist. As long as there is an element in the array that meets the conditions, some() will return true;
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 not, It returns false, and if one or more of them match, it returns true.
array.some(function callbackfn(Value,index,array),thisValue)
array: required parameter, an array object.
function callbackfn(value,index,array)
: A callback function, required parameters, can accept up to three parameters:
value: The value of the current array element, cannot be omitted.
index: The numeric index of the current array element.
array: The array object to which the current element belongs.
thisArg: Optional parameter, the 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. If an element is found that causes callbackfn to return true, the some() method returns true immediately. If the callback does not return true for any element, the some() method will return 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, etc.
Example: Check whether the values of the elements in the array are all odd numbers
If the some() method detects an even number, it returns true and prompts that they are not all odd numbers; if If no even numbers are detected, it will prompt that all are odd numbers.
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("全是奇数。"); }
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of What does some method in es6 do?. For more information, please follow other related articles on the PHP Chinese website!