Skip to content

Instantly share code, notes, and snippets.

@afitiskin
Last active December 17, 2015 10:09
Show Gist options
  • Save afitiskin/5593139 to your computer and use it in GitHub Desktop.
Save afitiskin/5593139 to your computer and use it in GitHub Desktop.
Add _.pickUp function to underscore object. This function (like _.pick function) returns a copy of the array, filtered to only have values for the whitelisted values (or array of values). For example: _.pickUp(['yes', 'no', 'yep', 'nope'], true, false, true, false) returns ['yes', 'yep'] Allow to pass second parameter as an array of values: _.pi…
(function () {
'use strict';
_.mixin({
pickUp: function () {
var args = Array.prototype.slice.call(arguments),
array = args.shift(),
result = [];
if (args.length === 1 && _.isArray(args[0])) {
args = args.shift();
}
_.each(args, function (value, key) {
if (value) {
result.push(array[key]);
}
});
return result;
}
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment