0

I have a JSON object with

parameters.projection = {"apples" : true}

parameters.projection = {"oranges" : true}

parameters.projection = {"lemons" : true}

I pass parameters to another function that makes the call to mongoDB

I need to combine those three elements above into a single projection objection that looks like

projection = {"apples" : true, "oranges" : true, "lemons" : true}

I have so far

var applyParameters = function(filter){

var params = {};

 for(var key in filter.projection){
    //What goes here to append them?
 }    

 return params;
};

No jquery and preferably no 3rd party libraries at all. Pure JavaScript please.

Community
  • 1
  • 1
Chauncey Philpot
  • 319
  • 1
  • 3
  • 14

1 Answers1

0

This worked for me

var applyParameters = function(filter){

//Manually build the JSON object as a string
var params = "{";
var projection = filter.projection;

for(var key in projection){
    if (projection.hasOwnProperty(key)) {

        params += '"' + key + '" : ' + projection[key] + ',';
    }
}
//Make sure the projection object wasn't empty.
if(params.length > 1){
    //Strip the last comma off
    params = params.substring(0, params.length-1);
}    

params += "}";

return JSON.parse(params);
};
Chauncey Philpot
  • 319
  • 1
  • 3
  • 14