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.

2
  • Does this works ? particularly the selected answer stackoverflow.com/questions/8430336/… Commented Jun 20, 2014 at 20:48
  • @JorgeOmarVazquez Not quite. It has to be specifically in the format above. I figured it out though. Commented Jun 20, 2014 at 21:05

1 Answer 1

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);
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.