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!
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());
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