Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create multiple query keys by URL & URLSearchParams

Tags:

javascript

url

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?

like image 309
kochizufan Avatar asked Oct 29 '25 12:10

kochizufan


1 Answers

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);
like image 153
boring91 Avatar answered Nov 01 '25 01:11

boring91



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!