JavaScript every () function test array size application instance

Source: Internet
Author: User

To test the size of an array:
Code:
--------------------------------------------------------------------------------

The code is as follows Copy Code

function Isbigenough (element, index, array) {
Return (element >= 10);
}
var passed = [5, 8, 130, 44].every (Isbigenough);
Passed is false
Passed = [A, Si, 130, 44].every (Isbigenough);
Passed is true
Results:
--------------------------------------------------------------------------------
[5, 8, 130, 44].every (Isbigenough) return: false
[130, 44].every (Isbigenough) return: True


Full instance

The code is as follows Copy Code
<script language= "JavaScript" type= "Text/javascript" >
if (! Array.prototype.every)
{Array.prototype.every=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 false;}
return true;}
function Isbigenough (Element,index,array) {return (element>=10);}
var passed=[12,5,8,130,44].every (Isbigenough);
Document.writeln ("[5, 8, 130, 44].every (Isbigenough) return:strong>");
Document.writeln (passed? ') True ': ' false ');
Document.writeln ("</strong><br/>");
Passed=[12,54,18,130,44].every (Isbigenough);
Document.writeln ("[A, Si, 130, 44].every (Isbigenough) return to:<strong>");d Ocument.writeln (passed? ') True ': ' false ');
Document.writeln ("</strong><br/>");
</script>


About the Every () function

Grammar
var allpassed = Array.every (callback[, Thisobject]);
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 FALSE, and if this element is found, every returns false, and every returns true if the callback function returns true 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.

Every 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.

Related Article

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.