Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove All Falsy Values From An Array With Javascript
#1
This is another little snippet i wrote to Remove all falsy values from an array.
Falsy values in JavaScript are

false, null,  "", 0,  undefined,  and NaN.




Code:
function bouncer(arr) {
  var me = [];
  var ppp = NaN;
var err = [false, undefined, NaN, 0, null, ""];
  for(var i = 0; i < arr.length; i++) {
    if(arr[i] !== false && arr[i] !==null && arr[i] !==undefined & arr[i] !== "" && arr[i] !== 0) {
        me.push(arr[i]);
      
    }
    
  }
  var final = me.filter(function(me) {
        return Number.isNaN(me) === false;
      });
  return final;
}
bouncer([1, null, NaN, 2, undefined]);



If you have a simpler way of achieving this pls post it below.
It takes so long to learn simplicity.
Facebook Share


Possibly Related Threads...
Thread
  HELP: How Can I Create A Javascript Ads Rotator For My Site
  Beginner Javascript Coding Mistakes To Avoid
  Concatenate Many Arrays Uniquely With Javascript
  Javascript Time Ago Function
  Shortest Code For Random Number Between 1 And N In Javascript
  Javascript Text Editor Right In Your Browser In One Line Of Code
  Getting Current Location With Javascript
  How To Check If Javascript Object Is Empty
  How To Create An Image Slider With Javascript
  Convert Numbers To Roman Numerals With Javascript




Users browsing this thread: 1 Guest(s)