Skip to content

Instantly share code, notes, and snippets.

@jimode
Last active April 13, 2022 09:05
Show Gist options
  • Save jimode/8995f560303bb3aa16d735b6b469a429 to your computer and use it in GitHub Desktop.
Save jimode/8995f560303bb3aa16d735b6b469a429 to your computer and use it in GitHub Desktop.
// Object.values().filter(Boolean)
// -------------------------------
// Video: https://courses.wesbos.com/account/access/5f69bb13ebaa134ef228e80e/view/455624835
// Filter undefinded toppings out
const tops = Object.values(toppings).filter(topping => topping !== undefined)
// can also be written as:
const tops = Object.values(toppings).filter(Boolean)
// e.g Boolean(undefined) => will return false
// e.g Boolean(0) => will return false
// e.g Boolean('') => will return false
// e.g Boolean(' ') => will return true (check this)
// Array.filter(Boolean)
// -------------------------------
// http://www.devign.me/javascript-tip-remove-falsy-items-out-of-an-array
// A quick tip to remove all falsy (false, null, undefined, 0, NaN or an empty string) items out of an array:
var a=[1, 2, "b", 0, {}, "", NaN, 3, undefined, null, 5];
var b=a.filter(Boolean); // [1,2,"b",{},3,5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment