Let's say I can get some data like below:
data = ["s=abc","t=123","a=567","b=789"]
How I can handle these data to obtain a url string like "?s=abc&t=123&a=567&b=789"
I write some code
for(i=0;i<data.length;i++){
data[i].split("=");
}
but stuck for a while.
Can anyone help me? by the way, the data.length may different each time.
Thanks.
How I can handle these data to obtain a url string like
"?s=abc&t=123&a=567&b=789"
Use join with & as glue. This will concat the elements of the array by using & as glue, then you can prepend ? to the resulting string.
var data = ["s=abc", "t=123", "a=567", "b=789"];
var params = '?' + data.join('&');
console.log(params);
document.write(params);
Note: If your parameters contains special characters you can use encodeURIComponent to encode it.
If you need the params to be url encoded, use $.param( data);
http://api.jquery.com/jquery.param/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With