Javascript some () function Usage Details, javascriptsome
Parameter description
Callback: the callback function to be executed for each array element.
ThisObject: this object defined when the callback function is executed.
Function Description
Execute a specified function (callback) for each element in the array until this function returns true. If this element is found, some returns true, if the callback function returns false after each element is executed, some returns false. It only executes the specified function for non-empty elements in the array, and no value is assigned or deleted elements are ignored.
The callback function can have three parameters: the current element, the index of the current element, and the current array object.
If the thisObject parameter is passed in, it will be used as the this object inside the callback function (callback). If it is not passed or is null, a global object will be used.
Copy codeThe Code is as follows:
<Script language = "JavaScript" type = "text/javascript">
If (! Array. prototype. some)
{
Array. prototype. some = function (fun/*, thisp */)
{
Var len = this. length;
If (typeof fun! = "Function ")
Throw new TypeError ();
Var thisp = arguments [1];
For (var I = 0; I <len; I ++)
{
If (I in this & fun. call (thisp, this [I], I, this ))
Return true;
}
Return false;
};
}
</Script>
Some does not change the original array. Remember: only the array elements passed in before the callback function is executed are valid. The elements added after the callback function is executed are ignored, during the execution of the callback function to the last element, if the array element is deleted or changed, the time when the callback function accesses the element shall prevail, the deleted element is ignored.
Check whether all array elements are greater than or equal to 10
Copy codeThe Code is as follows:
<Script language = "JavaScript" type = "text/javascript">
If (! Array. prototype. some)
{
Array. prototype. some = function (fun)
{
Var len = this. length;
If (typeof fun! = "Function ")
Throw new TypeError ();
Var thisp = arguments [1]; for (var I = 0; I <len; I ++)
{
If (I in this & fun. call (thisp, this [I], I, this ))
Return true ;}
Return false ;};
}
Function isBigEnough (element, index, array) {return (element >=10 );}
Var passed = [2, 5, 8, 1, 4]. some (isBigEnough );
Document. writeln ("[2, 5, 8, 1, 4]. some (isBigEnough): <strong> ");
Document. writeln (passed? 'True': 'false ');
Document. writeln ("</strong> <br/> ");
Passed = [12, 5, 8, 1, 4]. some (isBigEnough );
Document. writeln ("[12, 5, 8, 1, 4]. some (isBigEnough): <strong> ");
Document. writeln (passed? 'True': 'false ');
Document. writeln ("</strong> <br/> ");
</Script>
Function isBigEnough (element, index, array ){
Return (element> = 10 );
}
Var passed = [2, 5, 8, 1, 4]. some (isBigEnough );
// Passed is false
Passed = [12, 5, 8, 1, 4]. some (isBigEnough );
// Passed is true
Do you know something about some () functions? Can you leave me a message if you have any questions?