JavaScript Array to URLencoded
If you use jQuery, can use $.param(). Check here . Example of using
var myData = {'id' : '354313', 'fname' : 'Henry', 'lname' : 'Ford'};
var url = "https://stackoverflow.com?" + $.param(myData);
Output is
https://stackoverflow.com?id=354313&fname=Henry&lname=Ford
Also works with an array of objects (like from jQuery(form).serializeArray()) :
var myData = [{'id' : '354313'}, {'fname' : 'Henry'},{'lname' : 'Ford'}];
Try this:
var myData = {'id': '354313', 'fname':'Henry', 'lname': 'Ford'};
var out = [];
for (var key in myData) {
if (myData.hasOwnProperty(key)) {
out.push(key + '=' + encodeURIComponent(myData[key]));
}
}
out.join('&');
For an explanation about why use hasOwnProperty
, take a look at this answer to "How do I loop through or enumerate a JavaScript object?".
You can do something like this:
var myData = new Array('id=354313', 'fname=Henry', 'lname=Ford');
var url = myData.join('&');