Skip to content

Instantly share code, notes, and snippets.

@thedelk
Forked from RajaJaganathan/Usage.js
Last active November 11, 2022 20:55
Show Gist options
  • Save thedelk/e6a61bb3dcfd2924711e925b1bc65a43 to your computer and use it in GitHub Desktop.
Save thedelk/e6a61bb3dcfd2924711e925b1bc65a43 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;
});
},
});
const collections = [
{ id: 1, name: "xyz" },
{ id: 2, name: "ds" },
{ id: 3, name: "rtrt" },
{ id: 4, name: "nhf" },
{ id: 5, name: "qwe" },
];
const 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