Nodejs Array ForEach foreach( callback )

Here you can find the source of foreach( callback )

Method Source Code

// source from : http://www.naden.de/blog/javascript-foreach
Array.prototype.foreach = function( callback ) {
  for( var k=0; k<this .length; k++ ) {
    callback( k, this[ k ] );/*from   w  w w. j a va  2 s  . c  o  m*/
  }
}

Object.prototype.foreach = function( callback ) {
  for( var k in this ) {
    if(this.hasOwnProperty(k)) {
     callback( k, this[ k ] );
    }
  }
}

// Array Example
// var a = [ 'a', 'b', 'c', 'd' ];
// a.foreach( function( k, v ) {
//   document.writeln( k + ' ' + v );
// });
 
// // dieser Aufruf machte im Firefox 3 aus unerfindlichen Gr?Probleme
// [ 'a', 'b', 'c', 'd' ].foreach( function( k, v ) {
//   document.writeln( k + ' ' + v );
// });
 
// // so mag es auch der Firefox 3
// ;[ 'a', 'b', 'c', 'd' ].foreach( function( k, v ) {
//   document.writeln( k + ' ' + v );
// });


// Object Example
// var o = {
//   title:'red',
//   body:'yellow'
// };
 
// o.foreach( function( k, v ) {
//   document.writeln( k + ' ' + v );
// });

Related

  1. forEach2(a)
    Array.prototype.forEach2 = function (a) {
      for (var b = 0; b < this.length; ++b) a(this[b], b, this)
    };
    Array.prototype.map2 = function (a) {
      var l = this.length;
      var array = new Array(l),
        i = 0;
      for (; i < l; i++) {
        array[i] = a(this[i], i)
    ...
    
  2. forEachCallback(callback, finishCallback)
    'use strict';
    Array.prototype.forEachCallback = function(callback, finishCallback) {
      var current = 0;
      var self = this;
      var next = function() {
        if (!self) {
          console.log("Something went wrong...");
          throw('No self!');
          return;
    ...
    
  3. forEachSync(callback,compliteCallBack)
    var async = require('async');
    var worker = function(array,callback,compliteCallBack){
      var index = 0;
      var q = async.queue(function(item,cb){
        var cback = function(){
          index = index+1;
          cb();
        };
        callback(item,index,cback)
    ...
    
  4. forEachSync(callback,compliteCallBack)
    var async = require('async');
    var worker = function(array,callback,compliteCallBack){
      var index = 0;
      var q = async.queue(function(item,cb){
        var cback = function(){
          index = index+1;
          cb(); 
        };
        process.nextTick(function(){
    ...
    
  5. foreach( callback )
    Array.prototype.foreach = function( callback ) {
      for( var k=0; k<this .length; k++ ) {
        callback( k, this[ k ] );
    };
    
  6. foreach(callback)
    Array.prototype.foreach = function(callback) {
      for(var i in this) if(this.hasOwnProperty(i)) callback(this[i]);
      return this;
    
  7. foreach(callback)
    Array.prototype.foreach = function(callback)
        for (var i = 0; i < this.length; i++)
            callback(this[i])
    Array.prototype.getRandomIndex = function()
        var n = Math.floor(Math.random()*this.length)
        return n
    Array.prototype.getRandomElement = function()
        var n = this.getRandomIndex()
        return this[n]
    var Arrays = {
        remove: function(obj, arr) {
            var index = arr.indexOf(obj);
            if (index != -1)
                arr.splice(index, 1);
        },
        contains: function(obj, arr) {
            return arr.indexOf(obj) > -1;
        },
        addIfAbsent: function(obj, arr) {
            if (!Arrays.contains(obj, arr)) {
                arr.push(obj);
        },
        getRandomElement: function(arr) {
          var n = this.getRandomIndex(arr);
          return arr[n];
        },
        getRandomIndex: function(arr) {
         var n = Math.floor((Math.random()*arr.length));
          return n;
    };
    
  8. foreach(fn)
    Array.prototype.foreach = function(fn) {
      for (let i = 0; i < len; i++)
        fn(this[i], i, this);
    };
    
  9. foreach(func)
    Array.prototype.foreach = function(func) {
        var length = this.length;
        for ( var i = 0; i < length; i++ ) {
            func(this[i]);
    };