0

This might be a bit of a long shot but here we go. I've got 6 different variables that I need to create different url parameters. So for example:

var A
var B
var C
var D
var E
var F

Now I need to basically account for all options between those 6. So for example for var A I'd neeed something like:

   if (A != '' && B == '' && C == '' && D == '' && E == '' && F == '') {
            url += A ;
         }    
else if (A != '' && B != '' && C == '' && D == '' && E == '' && F == '') {
            url += A + "+" + B;
         }
else if (A != '' && B != '' && C != '' && D == '' && E == '' && F == '') {
            url += A + "+" + B + "+" + C;
         }

And so on. So basically I'd need to go through all the combinations like the above example, which I'm currently doing manually and which works fine, but there must be a better way of doing this?

1 Answer 1

5

You could filter the items, after taking them into an array and join with +.

result = [a, b, c, d, e, f].filter(Boolean).join('+');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.