\'bar\',
\'baz\'=>\'boom\',
\'cow\'=>\'milk\',
\'php\'=>\'hypertext processor\');
You can use the URLSearchParams
class:
var searchParametersData = {
"foo": "baz",
"bar": "foo"
};
var searchParameters = new URLSearchParams();
Object.keys(searchParametersData).forEach(function(parameterName) {
searchParameters.append(parameterName, searchParametersData[parameterName]);
});
console.log(searchParameters.toString()); // foo=baz&bar=foo
To get the parameters object from a search parameter string, use this:
var searchParametersString = "foo=baz&bar=foo";
var searchParameters = new URLSearchParams(searchParametersString);
var searchParametersData = {};
for (var searchParameter of searchParameters) {
searchParametersData[searchParameter[0]] = searchParameter[1];
}
console.log(searchParametersData); // {foo: "baz", bar: "foo"}