Last active
May 1, 2020 20:38
-
-
Save RajaJaganathan/23a8f437c2561e734ee0 to your computer and use it in GitHub Desktop.
lodash utility methods
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
_.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; | |
}); | |
} | |
}); |
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
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