javascript .filter() true booleans

前端 未结 6 1326
谎友^
谎友^ 2020-12-09 03:26
function bouncer(arr) {
  // Don\'t show a false ID to this bouncer.
    function a(b) {
      if(b !== false) {
        return b;
      }
    }

    arr = arr.filte         


        
6条回答
  •  醉话见心
    2020-12-09 04:21

    I came across this exercise and was playing about with it somewhat. It ended up helping me understand it a bit better, so if you may I will add a bit of clarity to this bouncer function.

    function bouncerOne(array){
    
    let truthy = array.filter(x => x);
    // This takes the .filter() and applies the anon function where it takes 
    // the value and returns the positive value. All values that are not Falsy including 
    // objects and functions would return True. 
    console.log("The Truth is:", truthy);//this Outputs the result to the console.
    let falsy = array.filter(x => !x); 
    //This takes the .filer() and applies the 
    //annon function where it takes the value and only returns items that ARE NOT True. 
    //So Falsy Values are taken. 
    console.log("The Falsy Values are",falsy);
    
    //The annon function of (x => x) & (x => !x)
    // really makes use of the strict equality power of JavaScript. 
    // Where simple conditional test can be carried out.
    };
    console.log(bouncerOne([7, "ate", "", false, 9,null,42,"Bingo",undefined,NaN]));
    //Returns: The Truth is: [ 7, 'ate', 9, 42, 'Bingo' ]
    //The Falsy Values are [ '', false, null, undefined, NaN ]
    

提交回复
热议问题