Last active
December 17, 2015 10:09
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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