Reputation: 2431
I want to create url value which have multiple key values in JavaScript.
If I do like this,
const url = new URL('http://example.com/hoge');
url.search = new URLSearchParams(['a': ['b', 'c'], 'd': 'e']);
console.log(url.href);
I achieved
http://example.com/hoge?a=b,c&d=e
But I want to have is
http://example.com/hoge?a=b&a=c&d=e
How can I get it?
Upvotes: 4
Views: 3597
Reputation: 2576
How about using query-string node module
const queryString = require('query-string');
let paramJson = {
'a' : ['b', 'c'],
'd' : 'e'
}
const url = new URL('http://example.com/hoge');
url.search = new URLSearchParams(queryString.stringify(paramJson));
console.log(url.href);
// http://example.com/hoge?a=b&a=c&d=e
Upvotes: 1
Reputation: 1121
You can do it this way:
const p = new URLSearchParams();
p.append('a', 'b');
p.append('a', 'c');
p.append('d', 'e');
const url = new URL('http://example.com/hoge');
url.search = p;
console.log(url.href);
Upvotes: 6