/ Published in: JavaScript
each in Array,It can operate each element in Array.
e.g.
var result = [1,2,3,4].each(function(x){return Math.sqrt(x);});
//it return sqrt in [1,2,3,4]'s each item
e.g.
var result = [1,2,3,4].each(function(x){return Math.sqrt(x);});
//it return sqrt in [1,2,3,4]'s each item
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
Array.prototype.each = function(fn){ return this.length? [fn(this[0])].concat(this.slice(1).each(fn)): []; }; //e.g. var arr = [1,2,3,4].each(function(x){return x*2;}); //alert(arr);//[2,3,6,8]