Nodejs Utililty Methods Array Compact

List of utility methods to do Array Compact

Description

The list of methods to do Array Compact are organized into topic(s).

Method

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;
};
...
compact()
Array.prototype.compact = function() {
  return this.select(function(value) {
    return value != null;
  });
};
compact()
Array.prototype.compact = function() {
  return this.filter(function(item) {
    return !!item;
  });
Object.prototype.compact = function() {
  var key, compacted;
  compacted = {};
  for(key in this) {
...
compact()
Array.prototype.compact = function () { 
  return this.filter((a) => a) 
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;
};
...
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;
};
compact()
Array.prototype.compact = function(){
    var compacted = [];
    for(var i = 0; i < this.length ; i++){
        if(this[i]){
            compacted.push(this[i]);
    };
    return compacted;
};
...
compact()
Array.prototype.compact = function () {
  return this.filter((value) => {
    return value;
  });
};
compact()
Array.prototype.compact = function() {
  return this.filter(function(i) {if (i) return true})
Array.prototype.collect = function(key) {
  return this.map(function(i){return i[key]})
Array.prototype.avg = function(key) {
  return eval(this.join('+')) / this.length;
Array.prototype.stats = function() {
  var avg = this.avg();
  var diff2 = this.map(function(i) {return (i-avg)*(i-avg)});
  var diff2avg = diff2.avg();
  return {
    diff2: diff2,
    var: diff2avg,
    std: Math.sqrt(diff2avg),
    avg: avg,
    total: this.length
Array.prototype.reduceToHash = function(key, val) {
  var tmp = {};
  this.forEach(function (i) {
    var k = i[key];
    var v = i[val];
    if (!tmp.hasOwnProperty(k))
      tmp[k] = [];
    tmp[k].push(v)
  });
  return tmp;
compact()
Array.prototype.compact = function() {
  return this.reduce(function(res, el) {
    if(el !== null) {
      res.push(el);
    };
    return res;
  }, [])