Merging same query parameters

I have a URL like so:

But there are multiple &league

I want to make it like this instead:,CAF_Confederations_Cup,Ligue_2

This is an AJAX request to PHP file, so I could split the , and check all leagues.

Any ideas on how to turn duplicate query parameters into commas?


Maybe this is what you need

let url = new URL('');

url.searchParams.set('league', url.searchParams.getAll('league').join(','));

let updatedUrl = url.toString(); //

, is not URL safe character, so it will need to be escaped (hence changed to %2C)