Eniz Gülek Eniz Gülek - 19 days ago 6
Javascript Question

How to convert object array to url string?

I am trying to generate javascript object array to urlstring like:

var array = [{'name': 'foo', 'value': '2'},
{'name': 'foo', 'value': '2,8'},
{'name': 'foo', 'value': '2,10,3'}
];

// url ==> foo=2&foo=2,8&foo=2,10,3

Answer

You can do this

var url = "";
array.forEach(function(e){
   url += e.name + "=" + e.value + "&";
});
url = url.trim("&");