1

I am having following sample code

 var obj = { a: {b:1, c:3 }, d :{f:5}}
 var string = "";
 for(var key in obj){
 for(var subkey in obj[key]){
 string += subkey + "="+ obj[key][subkey] + "&";
 //for last iteration "&" should not be added.
 }
 }
 console.log(string);

output is

b=1&c=3&f=5& 

Required Output

b=1&c=3&f=5
asked May 22, 2013 at 9:46
5
  • 1
    In this particular case, the easiest thing to do would be to simply take a substring of your output string, to remove the '&'. Commented May 22, 2013 at 9:49
  • If you're using jQuery, let that to jQuery. Don't compose the URL yourself Commented May 22, 2013 at 9:49
  • 1
    If it is for transmitting parameters and this is the final query string, just leave the & at the end. It won´t matter. Commented May 22, 2013 at 9:50
  • string += "&"+subkey + "="+ obj[key][subkey]; and at the end do ... string.substring(1) Commented May 22, 2013 at 9:52
  • Not a contribution to the question but to your case: are you sure your subkeys are unique (e.g. if subkey b in key a there cannot be a subkey b in key d)? Commented May 22, 2013 at 9:59

4 Answers 4

9

An alternative approach would be:

var obj = { a: {b:1, c:3 }, d :{f:5}},
 array = [];
for(var key in obj){
 for(var subkey in obj[key]){
 array.push(subkey + "="+ obj[key][subkey]);
 }
}
console.log(array.join('&'));
answered May 22, 2013 at 9:55
Sign up to request clarification or add additional context in comments.

Comments

4

If you're creating the query-part of a url, I'd go with the solution provided here: How to create query parameters in Javascript?

If not, just drop the encoding-part :)

Copy of relevant code:

// Usage:
// var data = { 'first name': 'George', 'last name': 'Jetson', 'age': 110 };
// var querystring = EncodeQueryData(data);
// 
function EncodeQueryData(data)
{
 var ret = [];
 for (var d in data)
 ret.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
 return ret.join("&");
}
answered May 22, 2013 at 9:57

1 Comment

You should be able to drop the first encodeURIComponent I should think, unless the field names have non-ascii chars
2

after for loop you can do like: string = string.slice(0,-1)

answered May 22, 2013 at 9:51

4 Comments

Or string += "&"+subkey + "="+ obj[key][subkey]; and at the end ... string.substring(1)
@Christoph sorry I commented correct but answered wrong, for mplungjan let me think
@mplungjan sorry :( I didn't understand, string.substring(1) will remove first char if I am not wrong, please example bit more I am also new learner web-languages ..
Yes it will, and I moved the & to the start.
1

For each iterarion append first & except for the first one.

var obj = { a: {b:1, c:3 }, d :{f:5}}
var string = "";
for(var key in obj){
 for(var subkey in obj[key]){
 if(string !== "") string += "&";
 string += subkey + "=" + obj[key][subkey];
 }
}
answered May 22, 2013 at 9:50

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.