JavaScript some () functional usage Detailed

Source: Internet
Author: User

Parameter description
Callback: The callback function to execute for each array element.
Thisobject: This object that is defined when the callback function is executed.

Function description
Each element in an array executes a specified function (callback) until this function returns True, and if this element is found, some returns true, and some returns false if the callback function returns false after each element is executed. It executes the specified function only for non-null elements in the array, and the elements that are not assigned or have been deleted 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 parameter thisobject is passed in, it will be treated as the this object within the callback function (callback), and if not passed or null, the global object will be used.

The code is as follows Copy Code
<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 here && Fun.call (Thisp, this[i], I, this))
return true;
}

return false;
};
}

</script>

Some does not change the original array, remember: Only the array elements that were passed in before the callback function was executed are ignored, and elements that are added after the callback function has been executed are omitted, and the array elements are deleted or changed when the callback function begins execution to the last element. The time that the element is accessed with the callback function will be ignored.

Check that all array elements are greater than or equal to 10

The code is as follows Copy Code
<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 ("[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 = [5, 8, 1, 4].some (Isbigenough);
Passed is true

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.