Skip to content

Instantly share code, notes, and snippets.

@RajaJaganathan
Last active May 1, 2020 20:38
Show Gist options
  • Save RajaJaganathan/23a8f437c2561e734ee0 to your computer and use it in GitHub Desktop.
Save RajaJaganathan/23a8f437c2561e734ee0 to your computer and use it in GitHub Desktop.
lodash utility methods
_.mixin({
'findByValues': function(collection, property, values) {
return _.filter(collection, function(item) {
return _.contains(values, item[property]);
});
},
'sformat': function(format) {
var args = Array.prototype.slice.call(arguments, 1);
return format.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined' ? args[number] : match;
});
}
});
Usage :
var collections = [
{id: 1, name: 'xyz'},
{id: 2, name: 'ds'},
{id: 3, name: 'rtrt'},
{id: 4, name: 'nhf'},
{id: 5, name: 'qwe'}
];
var filtered = _.findByValues(collections, "id", [1,3,4]);
_.sformat('I"m learning {0} but working in {1} {0}','ng','React');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment