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.