Javascript some () function Usage Details, javascriptsome

Source: Internet
Author: User

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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.