Skip to content
Advertisement

Javascript join multiple arrays with & but I get duplicates

I have this project in which I create a link with multiple arrays that are joined with & signs. It works perfectly, but when I get more than 3 arrays, it doesn’t. When I skip a couple of questions I get a & sign at the beginning of the string and when I only choose the first it adds one to the end which is bad. Also if I choose the first and the last array and skip the rest there a multiple & added between while I always just want 1.

I use this to join the arrays:

JavaScript

Quiz and this.url are the arrays and .prefix and stuff are keys of the arrays.

This is what I use to check the link string to remove the unwanted & signs:

JavaScript

And this is something I tried now:

JavaScript

But it also doesn’t really work.

Preview:

bd_shoe_size_ids=6621&&&

&&manufacturer_ids=5866

bd_shoe_size_ids=6598&&&manufacturer_ids=5866

It’s supposed to look like:

bd_shoe_size_ids=6598&manufacturer_ids=5866

Advertisement

Answer

It is better to avoid than to fix (the double &).

Remove the whole block that does push(""). This push is adding an entry to your array that you really don’t want to have at all, so just don’t push it.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement