Nodejs Array Remove removeEmpty()

Here you can find the source of removeEmpty()

Method Source Code

// Copyright 2018 Joseph W. May. All Rights Reserved.
///*from   w ww  .j  av a 2 s .c  o  m*/
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


/**
 * Returns the array with all empty, null, and undefined elements removed. This
 * does not modify the original array.
 * 
 * @returns {array} An array with no empty, null, or undefined elements.
 */
Array.prototype.removeEmpty = function() {
  var cleanArray = this.filter(function(n) {
      return (n !== undefined && n !== null && n !== '');
  });
  return cleanArray;
};

Related

  1. remByVal(val)
    Array.prototype.remByVal = function (val) {
        for (var i = 0; i < this.length; i++) {
            if (this[i] === val) {
                this.splice(i, 1);
                i--;
        return this;
    };
    ...
    
  2. removeByKey(index)
    Array.prototype.removeByKey = function(index) {
      var numToRemove = 1;
      return this.splice(index, numToRemove);
    };
    
  3. removeElement()
    Array.prototype.removeElement = function()
        var what, a = arguments, L = a.length, ax;
        while(L && this.length){
            what= a[--L];
            while((ax= this.indexOf(what))!= -1){
                this.splice(ax, 1);
        return this;
    };
    
  4. removeElementByProperty(chave,obj)
    Array.prototype.removeElementByProperty = function(chave,obj) {
        var i = this.length;
        while (i--) {
          if (angular.equals(this[i][chave], obj)) {
              this.splice(i,1);
    };
    
  5. removeEmpty()
    Array.prototype.removeEmpty = function () {
      for (var i = 0; i < this.length; ++i) {
        if (this[i].replace(/^\s*$/, "") === "") {
          this.splice(i--, 1);
      return this;
    };
    
  6. removeMultiple(values)
    Array.prototype.removeMultiple = function(values)
      for (var i = 0; i < values.length; i++)
        this.splice(this.indexOf(values[i]), 1);
        i--;
    
  7. removeNegatives()
    Array.prototype.removeNegatives = function() {
      var negCount = 0;
      for (var index = 0; index < this.length; index++) {
        if(this[index] >= 0){
          this[index - negCount] = this[index];
        } else {
          negCount++
      this.length = this.length-negCount;
    array2 = [-1,2.4,-3.1,4,-5.0,-6];
    array2.removeNegatives();
    console.log(array2);
    
  8. removeRange(start, end)
    Array.prototype.removeRange = function(start, end){
      this.splice(start, end-start);
    
  9. removeSame()
    Array.prototype.removeSame = function() {
      let theNewArray = [];
      this.map((value, index, array) => {
        if (array.indexOf(value) === index) {
          theNewArray.push(value);
      });
      return theNewArray;
    };
    ...