Converting a Javascript Dictionary To GET URL Parameters
I've had serveral instances in the recent past where I needed to take a javascript dictionary and convert it into a set of GET parameters to be used in a URI. Usually this happens when I'm building an AJAX request.
Anyway, since I've used this function over and over I thought I'd post it incase it helps any of you Googling for something similar. Here's the function...
Javascript to convert a javascript dictionary to GET parameters
function dictToURI(dict) {
var str = [];
for(var p in dict){
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(dict[p]));
}
return str.join("&");
}
You might use it in a case like this...
var myKeyValues = {
"key1" : "value1",
"key2" : "value2",
"key3" : "value3"
}
var ajaxURL = "http://api.mattcrampton.com/xyz?" + dictToURI(myKeyValues);
Which sets ajaxURL to...
http://api.mattcrampton.com/xyz?key1=value1&key2=value2&key3=value3
P.S. If you're pushing this to a static hosted site on S3, be sure to read my post on Cloudfront Invalidation
To: @mattccrampton
0
Other Posts
When exporting photos from a service like Flickr, perhaps after they've given notice that they're going to delete our photos if you don't subscribe to......
All Truthy and Falsy Javascript Values In Nodejs, every value has an associated boolean, true or false, value. For example, a null value has an......
Google Forcing Nest Cameras Visual Indicator Light To Be On Received the following email from Google today... Full email text... Recently, we shared our commitment......
Posting to Twitter with Python - Part Two: Posting Photos NOTE: This is part two of my posting to Twitter with Python tutorial. If you......
Doubleclick to open a file in VIM from OSX I use VIM for just about everything from note taking to coding to keeping track of......
Sign Into Gmail Without Signing Into Google Chrome Unfortunately, Google has made changes to Chrome since this blog post was posted which removed the options......