Lodash custom mixin

Lodash mixins

_.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');

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s