Array.max und Array.min

Posted by & filed under javascript.

Zwei kleine triviale Zusätze für meine Array-Erweiterungen…

Object.extend(Array.prototype, {
    max    : function() {
        var tmpMax = this[0];
        this.each(function(value){
            tmpMax = Math.max(value, tmpMax);
        }.bind(this));
        return tmpMax;
    },
    min    : function() {
        var tmpMin = this[0];
        this.each(function(value){
            tmpMin = Math.min(value, tmpMin);
        }.bind(this));
        return tmpMin;
    }
});

Erlaubt etwas wie:

[1,3,65,32,234,0,2].min()
// => 0
[1,3,65,32,234,0,2].max()
// => 234

Leave a Reply