Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Filter false values off an Array.
- * Why it works, simply `filter` evaluates the return of the iterator function to a boolean
- * if the value is true will return the current item and the opposite for false.
- * Misunderstanding happens when you think you will have to explictly return a boolean like
- * `return currentItem === 'someValue' ` but actually if you returned any value it will be converted to a boolean
- * just like doing `!!currentItem`
- * so you can have a shorter version of filtering like below
- **/
- var a = [0, 1, 2, 3, 'amin', '', false, NaN]
- a.filter(x => x); // [1, 2, 3, "ate"]
- a.filter(x => !x); // [0, "", false, NaN]
- // or that way
- a.filter(function(x){
- return x;
- });
- /* Using lodash
- * _.identity is a no-op function which just returns what is passed to it
- */
- _.filter(a, _.identity); // [1, 2, 3, "ate"]
- _.compact(a) // or just that simply.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement