Nodejs Array Each each(callback)

Here you can find the source of each(callback)

Method Source Code

/*//from  ww w.  j  a v a 2 s.c  o m

JavaScript provides an Array.prototype.forEach method that allows you to iterate over array values. For this exercise you will create your own array method called 'each'. It will be similar to the forEach method, except for one difference. If the callback function returns true then the loop will stop and no additional values will be iterated.

The following shows a contrived example of how this new method would be used:

var letters = ['a', 'b', 'c', 'd', 'e']
var allowedLetters = []
letters.each(function(letter, index){
  // break out of the loop if we reached a letter with the value 'd'
  if(letter == 'd') {
    return true;
  }
  allowedLetters.push(letter);   
})

// allowedLetters should equal ['a', 'b', 'c']

*/

Array.prototype.each = function(callback) {
  for ( var i = 0; i < this.length; i++ ){
    if (callback(this[i], i, this) !== undefined) return;
  }
};

Array.prototype.each = function(callback) {
  return this.some(callback);
};

Related

  1. each(callback)
    Array.prototype.each = function(callback) {
      for (var i = 0;i < this.length;i++) {
        callback(this[i], i);
    };
    
  2. each(callback)
    Array.prototype.each = function(callback) {
      for(var i = 0, l = this.length; i < l; i++) {
        callback(i, this[i]);
      return this;
    
  3. each(callback)
    Array.prototype.each = Array.prototype.each || function (callback) {
        var self = this;
        for (var i = 0; i < self.length; i++) {
            var item = self[i];
            callback(i, item);
    
  4. each(callback)
    Array.prototype.each = function (callback) {
      for (var i = 0; i < this.length; i++) {
        callback(this[i]);
      return this;
    };
    
  5. each(callback)
    Array.prototype.each = function(callback) {
        for (var i = 0 ; i < this.length ; i++ )
            callback(this[i]);
    Array.prototype.lastElement = function() {
        return (this.length === 0) ? undefined : this[this.length - 1];
    Array.prototype.verify = function(callback){ 
        var newArray = [];
    ...
    
  6. each(callback)
    Array.prototype.each = function (callback) {
      for(var i=0; i<this.length; i++){
        if ( callback(this[i]) == false ){
          break;
      return this;
    };
    
  7. each(cb)
    Array.prototype.each = function(cb) {
      for (var i = 0; i < this.length; i++) {
        cb(this[i])
    
  8. each(cb)
    Array.prototype.each = function(cb) {
        for (var i = 0; i < this.length; i += 1) {
            cb(this[i], i)
    };
    function stringify(arr) {
        var ret = [];
        arr.each(function(el) {
            console.log(el);
    ...
    
  9. each(cb, ctx)
    Array.prototype.each = function(cb, ctx) {
      for (var i = 0, l = this.length; i < l; i++) {
        if (cb.call((ctx === undefined) ? this[i] : ctx, i, this[i]) === false) {
          break;
    };