Nodejs Array Compact compact()

Here you can find the source of compact()

Method Source Code

// Compact (array)
// ---------------
// Removes falsy values
Array.prototype.compact = function() {
  return this.filter(function(item) {
    return !!item;
  });//from w  w w  . j a va2  s. com
}

// Compact (object)
// ----------------
// Removes falsy values/keys
Object.prototype.compact = function() {
  var key, compacted;
  compacted = {};
  for(key in this) {
    if(this[key]) {
      compacted[key] = this[key];
    }
  }
  return compacted;
}

Related

  1. compact()
    Array.prototype.compact = function() {
      var results = [];
      for (var i = 0, len = this.length; i < len; i++) {
        if (this[i] != null) {
          results.push(this[i]);
      return results;
    };
    ...
    
  2. compact()
    Array.prototype.compact = function() {
      return this.select(function(value) {
        return value != null;
      });
    };
    
  3. compact()
    Array.prototype.compact = function () { 
      return this.filter((a) => a) 
    
  4. compact()
    Array.prototype.compact = function()
        var a = [];
        for( var i = 0; i < this.length; i++ ) {
            if( i in this ) 
                if( this[ i ] != "" ) a.push( this[ i ] );
        return a;
    };
    ...
    
  5. compact()
    Array.prototype.compact = function(){
        var ca = [];
        for(var i in this){
            if (this.hasOwnProperty(i)) {
                if ( present(this[i]) ) {
                    ca.push(this[i]);
        return ca;
    };
    
  6. compact()
    Array.prototype.compact = function(){
        var compacted = [];
        for(var i = 0; i < this.length ; i++){
            if(this[i]){
                compacted.push(this[i]);
        };
        return compacted;
    };
    ...