> Some of the prototype.js functions which I use quite a bit are:
> include(), inject(), collect(), findAll(), and detect() which are
documented at
>
http://encytemedia.com/blog/articles/2005/12/07/prototype-meets-ruby-a-look-
at-enumerable-array-and-hash
All of those look easy to implement, here would be my (untested)
implementations as Array methods:
Array.prototype.include = function(v) {
for ( var i=0; i < this.length; i++ )
if ( this[i] == v ) return true;
return false;
};
Array.prototype.inject = function(v,f)
{
for ( var i=0; i < this.length; i++)
v = f(v, this[i]);
return v;
};
Array.prototype.collect = function(f) {
var r = [];
for ( var i=0; i < this.length; i++ )
r[i] = f(this[i], i);
return r;
};
Array.prototype.findAll = function(g) {
var r = [];
for ( var i=0; i < this.length; i++ )
if ( f(this[i], i) ) r.push(this[i]);
return r;
};
Array.prototype.detect = function(f) {
for ( var i=0; i < this.length; i++ )
if ( f(this[i], i) ) return this[i];
return undefined; //??
};
>From the documentation it wasn't clear to me what .detect returned if no
element was found.
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/