Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Opera, Safari, Firefox and Chrome now all share a set of enhanced Array methods for optimizing many common loops.</p> <p>You may not need all of them, but they can be very useful, or would be if every browser supported them.</p> <p>Mozilla Labs published the algorithms they and <a href="http://en.wikipedia.org/wiki/WebKit" rel="noreferrer">WebKit</a> both use, so that you can add them yourself.</p> <p><strong>filter</strong> returns an array of items that satisfy some condition or test.</p> <p><strong>every</strong> returns true if every array member passes the test.</p> <p><strong>some</strong> returns true if any pass the test.</p> <p><strong>forEach</strong> runs a function on each array member and doesn't return anything.</p> <p><strong>map</strong> is like forEach, but it returns an array of the results of the operation for each element.</p> <p>These methods all take a function for their first argument and have an optional second argument, which is an object whose scope you want to impose on the array members as they loop through the function.</p> <p>Ignore it until you need it.</p> <p><strong>indexOf</strong> and <strong>lastIndexOf</strong> find the appropriate position of the first or last element that matches its argument exactly.</p> <pre><code>(function(){ var p, ap= Array.prototype, p2={ filter: function(fun, scope){ var L= this.length, A= [], i= 0, val; if(typeof fun== 'function'){ while(i&lt; L){ if(i in this){ val= this[i]; if(fun.call(scope, val, i, this)){ A[A.length]= val; } } ++i; } } return A; }, every: function(fun, scope){ var L= this.length, i= 0; if(typeof fun== 'function'){ while(i&lt;L){ if(i in this &amp;&amp; !fun.call(scope, this[i], i, this)) return false; ++i; } return true; } return null; }, forEach: function(fun, scope){ var L= this.length, i= 0; if(typeof fun== 'function'){ while(i&lt; L){ if(i in this){ fun.call(scope, this[i], i, this); } ++i; } } return this; }, indexOf: function(what, i){ i= i || 0; var L= this.length; while(i&lt; L){ if(this[i]=== what) return i; ++i; } return -1; }, lastIndexOf: function(what, i){ var L= this.length; i= i || L-1; if(isNaN(i) || i&gt;= L) i= L-1; else if(i&lt; 0) i += L; while(i&gt; -1){ if(this[i]=== what) return i; --i; } return -1; }, map: function(fun, scope){ var L= this.length, A= Array(this.length), i= 0, val; if(typeof fun== 'function'){ while(i&lt; L){ if(i in this){ A[i]= fun.call(scope, this[i], i, this); } ++i; } return A; } }, some: function(fun, scope){ var i= 0, L= this.length; if(typeof fun== 'function'){ while(i&lt;L){ if(i in this &amp;&amp; fun.call(scope, this[i], i, this)) return true; ++i; } return false; } } } for(p in p2){ if(!ap[p]) ap[p]= p2[p]; } return true; })(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload