controller.js
function(fields, data) {
    if (!_.isString(fields)) {
      return data;
    }

    // If a field is specified as `foo.bar` or `foo.bar.baz`,
    // Convert it to just `foo`
    var map = {};
    _.forEach(fields.split(','), function(field) {
      map[field.split('.')[0]] = 1;
    });

    var keys = _.keys(map);
    if (_.isArray(data)) {
      data = _.map(data, function(object) {
        return _.pick(object, keys);
      });
    } else if (_.isObject(data)) {
      data = _.pick(data, keys);
    }

    return data;
  }
function(fields, data) {
    if (!_.isString(fields)) {
      return data;
    }

    // If a field is specified as `foo.bar` or `foo.bar.baz`,
    // Convert it to just `foo`
    var map = {};
    _.forEach(fields.split(','), function(field) {
      map[field.split('.')[0]] = 1;
    });

    var keys = _.keys(map);
    if (_.isArray(data)) {
      data = _.map(data, function(object) {
        return _.pick(object, keys);
      });
    } else if (_.isObject(data)) {
      data = _.pick(data, keys);
    }

    return data;
  }
function ( fields , data ) { if ( ! _ . isString ( fields ) ) { return data ; } // If a field is specified as `foo.bar` or `foo.bar.baz`, // Convert it to just `foo` var map = { } ; _ . forEach ( fields . split ( ',' ) , function ( field ) { map [ field . split ( '.' ) [ 0 ] ] = 1 ; } ) ; var keys = _ . keys ( map ) ; if ( _ . isArray ( data ) ) { data = _ . map ( data , function ( object ) { return _ . pick ( object , keys ) ; } ) ; } else if ( _ . isObject ( data ) ) { data = _ . pick ( data , keys ) ; } return data ; }
Modify `data` to only contain keys that are specified in `fields`

@param {Object} fields
@param {Object|Array} data
@return {Object|Array}
Modify data to only contain keys that are specified in fields