Sugar Arrays: Porting JavaScript 1.6 Array methods

Download Full Source (2.2k) | Minified (1.4k)

Array.prototype.forEach

Array.prototype.forEach = function(fn, thisObj) {
    var scope = thisObj || window;
    for ( var i=0, j=this.length; i < j; ++i ) {
        fn.call(scope, this[i], i, this);
    }
};

Test Case:

$$('h2').forEach(
    function(el, i, ar) {
        el.style.color = 'yellow';
    }
);

Result: If every sub-heading on this page is yellow, then forEach has succeeded.


Array.prototype.every

Array.prototype.every = function(fn, thisObj) {
    var scope = thisObj || window;
    for ( var i=0, j=this.length; i < j; ++i ) {
        if ( !fn.call(scope, this[i], i, this) ) {
            return false;
        }
    }
    return true;
};

Test Case:

var result = ['foo', 'foo', 'foo'].every(function(el, i, ar) {
	return hasFoo(el);
});

Result is:

var result = ['foo', 'bar', 'foo'].every(function(el, i, ar) {
	return hasFoo(el);
});

Result is:


Array.prototype.some

Array.prototype.some = function(fn, thisObj) {
    var scope = thisObj || window;
    for ( var i=0, j=this.length; i < j; ++i ) {
        if ( fn.call(scope, this[i], i, this) ) {
            return true;
        }
    }
    return false;
};

Test Case:

var result = ['bar', 'bar', 'bar'].some(function(el, i, ar) {
	return hasFoo(el);
});

Result is:

var result = ['bar', 'foo', 'bar'].some(function(el, i, ar) {
	return hasFoo(el);
});

Result is:


Array.prototype.map

Array.prototype.map = function(fn, thisObj) {
    var scope = thisObj || window;
    var a = [];
    for ( var i=0, j=this.length; i < j; ++i ) {
        a.push(fn.call(scope, this[i], i, this));
    }
    return a;
};

Test Case:

var result = ['dustin', 'robert', vince'].map(function(el, i, ar) {
	return el.toUpperCase();
});

Result is:


Array.prototype.filter

Array.prototype.filter = function(fn, thisObj) {
    var scope = thisObj || window;
    var a = [];
    for ( var i=0, j=this.length; i < j; ++i ) {
        if ( !fn.call(scope, this[i], i, this) ) {
            continue;
        }
        a.push(this[i]);
    }
    return a;
};

Test Case:

var result = ['dustin', 'robert', 'virus', 'vince'].filter(
	function(el, i, ar) {
        if ( el !== 'virus' ) {
            return el;
        }
    }
);

Result is:


Array.prototype.indexOf

Array.prototype.indexOf = function(el, start) {
    var start = start || 0;
    for ( var i=0; i < this.length; ++i ) {
        if ( this[i] === el ) {
            return i;
        }
    }
    return -1;
};

Test Case:

var a = [2, 5, 9],
	index;
index = a.indexOf(2); // index is 0
index = a.indexOf(7); // index is -1

Results are:


Array.prototype.lastIndexOf

Array.prototype.lastIndexOf = function(el, start) {
    var start = start || this.length;
    if ( start >= this.length ) {
        start = this.length;
    }
    if ( start < 0 ) {
         start = this.length + start;
    }
    for ( var i=start; i >= 0; --i ) {
        if ( this[i] === el ) {
            return i;
        }
    }
    return -1;
};

Test Case:

var a = [2, 5, 9, 2],
    i;
i = a.lastIndexOf(2); // i is: 3
i = a.lastIndexOf(7); // i is: -1
i = a.lastIndexOf(2, 3); // i is: 3
i = a.lastIndexOf(2, 2); // i is: 0
i = a.lastIndexOf(2, -2); // i is: 0
i = a.lastIndexOf(2, -1); // i is: 3

Results are:

Brought to you by Dustin Diaz: ./with Imagination

Creative Commons 2.5 2006