Apply the function {handler} to each element in the array - Node.js Array

Node.js examples for Array:Array Value

Description

Apply the function {handler} to each element in the array

Demo Code


/**/* ww  w .  j  ava2  s .  c  o  m*/
 * Apply the function {handler} to each element in the array
 * Return false in the {handler} to break the cycle.
 * @param {Function} handler
 * @version 1.0.1
 * @date August 20, 2010
 * @since June 30, 2010
 * @package jquery-sparkle {@link http://www.balupton/projects/jquery-sparkle}
 * @author Benjamin "balupton" Lupton {@link http://www.balupton.com}
 * @copyright (c) 2009-2010 Benjamin Arthur Lupton {@link http://www.balupton.com}
 * @license MIT License {@link http://creativecommons.org/licenses/MIT/}
 */
Array.prototype.each = function(handler){
  for (var i = 0; i < this.length; ++i) {
    var value = this[i];
    if ( handler.apply(value,[i,value]) === false ) {
      break;
    }
  }
  return this;
}

Related Tutorials