Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two URLSearchParams()

Tags:

javascript

Quick question:

Is it possible to combine two instances of URLSearchParams?

const params1 = new URLSearchParams();
const params2 = new URLSearchParams();

const finalParms = mergeThis(params1, params2) <- do this?

This question has been answered for angular but in plain JavaScript URLSearchParams has no member called .appendAll()

Thanks!

like image 402
Xen_mar Avatar asked Oct 23 '25 16:10

Xen_mar


1 Answers

There's no built-in method to do that, but you can simply loop over the entries and append them:

const params1 = new URLSearchParams("?foo=foo&bar=bar");
const params2 = new URLSearchParams("?baz=baz&nii=nii");
for (let [key, val] of params2.entries()) {
  params1.append(key, val);
}
console.log(params1.toString());
like image 166
Quentin Avatar answered Oct 25 '25 05:10

Quentin



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!