Array.pluck #1064

Merged
merged 1 commit into from Nov 4, 2011

2 participants

@ghost

Good complement to Array.invoke, this method is in Prototype and ExtJS (off the top of my head; there could be others).

Thomas Allen Array.pluck 6d78fa9
@cpojer
MooTools member

Sure whynot.

@cpojer cpojer merged commit df02433 into mootools:master Nov 4, 2011
@arian
MooTools member

Very nice!

Alternative implementation without relying on .map and function calls, which should be a bit faster:


Array.implement({

    pluck: function(property){
        var length = this.length >>> 0, results = new Array(length);
        for (var i = 0; i < length; i++){
            if (i in this) results[i] = this[i][property];
        }
        return results;
    }

});

jsperf: http://jsperf.com/pluck

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment