Javascript Array trimEnds(f)

Description

Javascript Array trimEnds(f)



Array.prototype.trimEnds = function(f) {
    // find leftmost element to keep
    var i = 0//from   w ww.  j  a va 2s . c om
    while (i < this.length && f(this[i]))
      i++
    // find rightmost element to keep
    var j = this.length - 1
    while (j > i && f(this[j]))
      j--
    // return new array
    return this.slice(i, j + 1)
};



PreviousNext

Related