Lodash to filter non zero values in nested objects

this is my lhr.json:

[ { roles: 'dashboards', allows: [ [Object] ] },
  { roles: 'dashboards_t4', allows: [ [Object] ] },
  { roles: 'dashboards_t4_checkin', allows: [ [Object] ] },
  { roles: 'dashboards_t4_immigration', allows: [ [Object] ] },
  { roles: 'tools', allows: [ [Object] ] },
  { roles: 'tools_forecast_uploader', allows: [ [Object] ] },
  { roles: 'tools_data_explorer', allows: [ [Object] ] },
  { roles: 'reports', allows: [ [Object] ] },
  { roles: 'acl_admin', allows: [ [Object] ] },
  { roles: 'superuser', allows: [] },
  { roles: 'integrated', allows: [] },
  { roles: 'capacity', allows: [] },
  { roles: 'airport', allows: [] },
  { roles: 'hal', allows: [] },
  { roles: 'security', allows: [] },
  { roles: 'passenger', allows: [] },
  { roles: 'engineering', allows: [] },
  { roles: 'tts', allows: [] },
  { roles: 'border_force', allows: [] } ]

how to use lodash filter to get only the values where ‘allows’ is not empty?

Tried:

var filter = _.filter(lhr.roles, ‘allows’, _.isEmpty() );

var compact = _.compact(lhr.roles)

But still not working

I’d use the filter method directly on the array:

const compact = lhr.roles.filter(obj => {
  if (obj.allows && obj.allows.length === 0) return false;
  return true;
});

Again first to respond Rob! Big cookie

1 Like