Skip to content

Instantly share code, notes, and snippets.

@vibhasbhingarde
Created September 12, 2018 07:42
Show Gist options
  • Save vibhasbhingarde/e04f6dee60fc51fb21ea7e19ee94613c to your computer and use it in GitHub Desktop.
Save vibhasbhingarde/e04f6dee60fc51fb21ea7e19ee94613c to your computer and use it in GitHub Desktop.
Reject undefined/Null element from array in short getting rid of conditional push
//https://triin.net/2010/01/13/Fighting_the_uglyness_of_Array.push()
var buttons = [
new OpenButton(),
isPayingCustomer() ? new ExportButton() : null,
new SaveButton(),
new DeleteButton()
].compact();
Array.prototype.compact = function() {
return this.filter(function(x) {
return x !== null && x !== undefined;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment