Eniz Gülek Eniz Gülek - 6 months ago 38
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 Source

You can do this

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