Skip to content

Instantly share code, notes, and snippets.

@nltesown
Last active August 29, 2015 14:22
Show Gist options
  • Save nltesown/07631fa0f4af05fbdf77 to your computer and use it in GitHub Desktop.
Save nltesown/07631fa0f4af05fbdf77 to your computer and use it in GitHub Desktop.
Takes a jQuery object of checkbox elements and returns info about the checked ones
// Takes a jQuery object of checkbox elements and returns:
// - An array of the IDs of the checked ones
// - (if asCollection is true) A collection of the checked ones where key=ID, value=true
// Dependency: jQuery, underscore/lodash
function readCheckboxes($cb, asCollection) {
return _($cb.filter(":checked"))
.map(function(item) {
return item.id;
})
.thru(function(item) {
return (!!asCollection ? _(item).zipObject().mapValues(function() {
return true;
}).value() : item);
})
.value();
}
@nltesown
Copy link
Author

btw I really love the functional style where the body of a function is only a return statement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment