user5671335 user5671335 - 1 month ago 8
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

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

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

Fiddle here: https://jsfiddle.net/W4QfJ/3160/

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