Nodejs Array ForEach foreach( callback )

Here you can find the source of foreach( callback )

Method Source Code

Array.prototype.foreach = function( callback ) {
  for( var k=0; k<this .length; k++ ) {
    callback( k, this[ k ] );/*  w w w . ja va  2  s  .  com*/
  }
};

Related

  1. forEach1( callback )
    Array.prototype.forEach1 = function ( callback ) {
            for (var j = 0, k ; k=this[j++]; ) {  callback(k,j,this) ; }
    Array.prototype.forEach2 = function ( callback ) {
            for (var j = 0 ; j < this.length; j++ ) {  callback(this[j],j,this) ; }
    Array.prototype.forEach3 = function ( callback ) {
            var j = 0 ; while( this[j++] ) {  callback(this[j],j,this) ; }
    var a = [1,2,3,4,5,6,7,8,9,0] ;
    function unit ( method ) 
       var s = [] ;
        a[method]( function (v,i,a) { s.push(i+":"+v); } )
      return s ;
    function loop ( size, method ) {
    var t1 = new Date() ;
     while ( size--) { unit(method); }
    return (new Date()) - t1 ;
    loopall = function ( ) {
     var s = [] ;
     var j = 0, method ; while ( method = arguments[j++] ) {
      s.push( method + ": " +  loop( 1e4 , method )  + " ms " ) ;
       return s ;
    } ( "forEach1", "forEach2", "forEach3" ) .join("\n") ;
    
  2. 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)
    ...
    
  3. 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;
    ...
    
  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();
        };
        callback(item,index,cback)
    ...
    
  5. 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(){
    ...
    
  6. foreach( callback )
    Array.prototype.foreach = function( callback ) {
      for( var k=0; k<this .length; k++ ) {
        callback( k, this[ k ] );
    Object.prototype.foreach = function( callback ) {
      for( var k in this ) {
        if(this.hasOwnProperty(k)) {
         callback( k, this[ k ] );
    ...
    
  7. foreach(callback)
    Array.prototype.foreach = function(callback) {
      for(var i in this) if(this.hasOwnProperty(i)) callback(this[i]);
      return this;
    
  8. 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;
    };
    
  9. foreach(fn)
    Array.prototype.foreach = function(fn) {
      for (let i = 0; i < len; i++)
        fn(this[i], i, this);
    };