Nodejs Array ForEach forEach(cb)

Here you can find the source of forEach(cb)

Method Source Code

'use strict';/*w  w  w  . j av  a2  s .  c o  m*/

Array.prototype.forEach = function(cb) {
  for(let i = 0 ; i < this.length ; i++) {
    cb(this[i]);
  }
};

Related

  1. forEach(callback, context)
    Array.prototype.forEach = Array.prototype.forEach || function (callback, context) {
      context = context || window;
      var l = this.length;
      for (var i = 0; i < l; i++)
        callback.call(context, this[i], i, this);
    };
    
  2. forEach(callback, context)
    Array.prototype.forEach = function (callback, context) {
        for (var index in this) {
            var item = this[index];
            if (!callback(index, item, context)) {
                break;
    };
    
  3. forEach(callback, thisArg)
    Array.prototype.forEach = function (callback, thisArg) {
      if (typeof callback !== 'function') {
        throw new TypeError(callback + ' is not a function.');
      var len = this.length;  
      for (var i = 0; i < len; i++) {
        callback.call(thisArg, this[i], i, this);
    };
    ...
    
  4. forEach(cb)
    Array.prototype.forEach = function(cb){
      for(var k in this) cb(this[k]);
    };
    
  5. forEach(cb)
    Array.prototype.forEach = function(cb) {
        for (var i = 0; i < this.length; i++) {
            cb(this[i]);
    };
    
  6. forEach(fn)
    Array.prototype.forEach  = function(fn) {
      for (var i = 0; i < this.length; i++) fn(this[i]);
    
  7. forEach(fn)
    Array.prototype.forEach=function(fn){
        var arr=this;
        var length=this.length;
        for(var i=0;i<length;i++){
            fn(arr[i],i,arr);
    
  8. forEach(fn)
    Array.prototype.forEach = function(fn)
        for (var i=0;i<this.length;i++)
            fn(this[i]);
    
  9. forEach(fn, context)
    Array.prototype.forEach = function(fn, context) {
      if (typeof fn != "function") {
        throw new TypeError(fn + " is not a function");
      if (typeof context === 'undefined') {
        context = this;
      for (var i = 0, l = this.length; i < l; ++i) {
        fn.call(context, this[i], i, this);
    ...