Nodejs Array Each each(cb)

Here you can find the source of each(cb)

Method Source Code

Array.prototype.each = function(cb) {
    for (var i = 0; i < this.length; i += 1) {
        cb(this[i], i)//from   w  ww.ja v  a 2 s  .c o m
    }
};

function stringify(arr) {
    var ret = [];
    arr.each(function(el) {
        console.log(el);
        var current = '[' + el[0] + ', ';
        if (typeof el[1] === 'object') {
            current += stringify(el[1]);
        } else {
            current += el[1];
        }
        current += ']';
        ret.push(current);
    });
    return '[' + ret.join(', ') + ']';
}

function toPairs(obj) {
    var arrs = [];
    for (var p in obj) {
        if (typeof obj[p] === 'object') {
            // toPairs(obj[p]);
            arrs.push([p, toPairs(obj[p])]);
        } else {
            arrs.push([p, obj[p]]);
        }
    }
    return stringify(arrs);
};

toPairs({
    a: {
        b: 'c'
    }
});

Related

  1. each(callback)
    Array.prototype.each = function (callback) {
      for (var i = 0; i < this.length; i++) {
        callback(this[i]);
      return this;
    };
    
  2. 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 = [];
    ...
    
  3. each(callback)
    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);
    };
    
  4. each(callback)
    Array.prototype.each = function (callback) {
      for(var i=0; i<this.length; i++){
        if ( callback(this[i]) == false ){
          break;
      return this;
    };
    
  5. each(cb)
    Array.prototype.each = function(cb) {
      for (var i = 0; i < this.length; i++) {
        cb(this[i])
    
  6. 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;
    };
    
  7. each(command)
    "use strict";
    Array.prototype.each = function(command){
      for(var i = 0; i < this.length; i++){
        command(this[i]);
      return this;
    };
    
  8. each(f)
    Array.prototype.each= function(f) {
      i= -1;
      while(++i < this.length) 
        f(this[i]);
    };
    
  9. each(f)
    Array.prototype.each = function(f) {
      for (var i=0; i<this.length; i++) {
        f(this[i])