javascript - Join strings with a separator, ignoring empty strings -


I am trying to create a variable.

If it is not null or empty or undefined I do not want to add it to the @ symbol:

< Pre> var T1 = 'popcorn' var T2 = 'ice cream' var T3 = ''; Var T4 = ''; If (T1! = '' || T2! = '' || T3! = '' || T4! = '') Reicastrate = T1 + '@' + T2 + '@' + T 3 + '@' + T4; Warning (reqstr);

The output is now:

popcorn @ ice cream @@

if the variable is empty I @

P>

not clear Q; Try it: [T1, T2, T3, T4]. Filler (boolean) .join ('@');

And if you want to allow numbers in your variables, then you have to do a bit more work:

  [T1, T2, T3, T4 ] .filter (function (x) {back type x === 'number' || x;}) ('@');  

To allow more numbers, but NaN :

  filter [T1, T2, T3, T4] . Filter (function (x) {return x === 0 || X;}). Joining ('@');  

etc., depending on the specification.


Comments