In this post, we will see how to resolve Shorthand for conditionally creating an array out of an optional value?
Question:I have a function that optionally accepts an array. I have other variables that may be
undefined, but if they are defined I would like to create an array from it:
tempvariable with an an array of
undefined? I tried to do the following but didn’t achieve it:
Best Answer:As it was pointed out in the comments, just create a helper function for this:
Also, thanks to pilchard in the comments for noting that it is better to use a generic, rather than just the
stringtype in your question or the
anytype. Also, JDB for noting you must use
!== undefined, rather than simply checking for a falsy value (as the original question does).
If you have better answer, please add a comment about this, thank you!