user5671335 user5671335 - 10 months ago 61
Javascript Question

Lodash reject get return object

var obj = {a: [], b: [1,2], c: [], d: [1]};

How do I get a non-empty array of objects like the following:

{b: [1,2], d: [1]}

Answer Source

You can do what you are after, using pickBy().

var result = _.pickBy(obj, function(val){
    return val.length > 0;

Fiddle here:

Note: Unlike filter() and reject(), this returns an object, keeping your original structure (rather than an array).