dist/objectSubset.js
function traverseThroughObject(lookup, doc, output) {

    if (!output) {
      output = {};
    }

    for (var key in lookup) {

      if (doc.hasOwnProperty(key)) {

        if(Object.keys(lookup[key]).length === 0) {

          output[key] = doc[key];

        } else {

          if (!output.hasOwnProperty(key)) {

            if (Object.prototype.toString.call(doc[key]) === '[object Array]') {

              output[key] = [];
              for (var i = 0, _len = doc[key].length; i < _len; i++) {
                var field = {};
                output[key].push(field);
                traverseThroughObject(lookup[key], doc[key][i], field);
              }

            } else {

              output[key] = {};
              traverseThroughObject(lookup[key], doc[key], output[key]);

            }

          }

        }

      }

    }

    return output;

  }
function traverseThroughObject(lookup, doc, output) {

    if (!output) {
      output = {};
    }

    for (var key in lookup) {

      if (doc.hasOwnProperty(key)) {

        if(Object.keys(lookup[key]).length === 0) {

          output[key] = doc[key];

        } else {

          if (!output.hasOwnProperty(key)) {

            if (Object.prototype.toString.call(doc[key]) === '[object Array]') {

              output[key] = [];
              for (var i = 0, _len = doc[key].length; i < _len; i++) {
                var field = {};
                output[key].push(field);
                traverseThroughObject(lookup[key], doc[key][i], field);
              }

            } else {

              output[key] = {};
              traverseThroughObject(lookup[key], doc[key], output[key]);

            }

          }

        }

      }

    }

    return output;

  }
function traverseThroughObject ( lookup , doc , output ) { if ( ! output ) { output = { } ; } for ( var key in lookup ) { if ( doc . hasOwnProperty ( key ) ) { if ( Object . keys ( lookup [ key ] ) . length === 0 ) { output [ key ] = doc [ key ] ; } else { if ( ! output . hasOwnProperty ( key ) ) { if ( Object . prototype . toString . call ( doc [ key ] ) === '[object Array]' ) { output [ key ] = [ ] ; for ( var i = 0 , _len = doc [ key ] . length ; i < _len ; i ++ ) { var field = { } ; output [ key ] . push ( field ) ; traverseThroughObject ( lookup [ key ] , doc [ key ] [ i ] , field ) ; } } else { output [ key ] = { } ; traverseThroughObject ( lookup [ key ] , doc [ key ] , output [ key ] ) ; } } } } } return output ; }
traverseThroughObject is traversing through the object
by the fields described in lookup and filling the
output object accordingly in the process.
traverseThroughObject is traversing through the object by the fields described in lookup and filling the output object accordingly in the process .