Nodejs Array Compact compact(deleteValue)

Here you can find the source of compact(deleteValue)

Method Source Code

Array.prototype.compact = function(deleteValue) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == deleteValue) {
      this.splice(i, 1);//from  ww  w. j a v a2s.  c om
      i--;
    }
  }
  return this;
};

Related

  1. compact()
    Array.prototype.compact = function () {
      return this.filter((value) => {
        return value;
      });
    };
    
  2. 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;
    
  3. compact()
    Array.prototype.compact = function() {
      return this.reduce(function(res, el) {
        if(el !== null) {
          res.push(el);
        };
        return res;
      }, [])
    
  4. compact()
    Array.prototype.compact = function() {
        return this.filter(function (i) {
            return !!i;
        });
    };
    
  5. compact()
    Array.prototype.compact = function() {
        var comp = [];
        for (var i = 0;i < this.length; i++) {
            if (this[i]) {
                comp.push(this[i]);
        return comp;
    
  6. compact(fn)
    Array.prototype.compact = function(fn) {
      for (var i = 0; i < this.length; i++)
        if (this[i] == null) {
          this.splice(i, 1);
      return this;
    
  7. compacted()
    Array.prototype.compacted = function () {
        var tmp = [];
        for (var i = 0; i < this.length; i++) {
            if (this[i] !== undefined && this[i] !== null && this[i] !== false) {
                tmp.push(this[i]);
        return tmp;
    };
    ...
    
  8. compacted()
    Array.prototype.compacted = function () {
        var tmp = [];
        for (var i = 0; i < this.length; i++) {
            if (this[i] !== undefined && this[i] !== null && this[i] !== false) {
                tmp.push(this[i]);
        return tmp;
    };
    ...