It's not feasible to use the (reasonably¹) switch
as a statement in this context. The location where you intend to place it only accepts expressions, not statements.
While it is possible to employ a complex series of conditional operators (? :
), it is generally recommended to determine the value before constructing your object literal. Complicated chains of conditions can be difficult to comprehend and troubleshoot.
My suggestion would be to execute the switch
, save the result in a variable, and then incorporate the variable into the object literal. (I could provide an example, but it's unclear what purpose you want the switch
to serve, or how your current code functions.)
¹ Alternatively, you might consider creating an inline function with a switch
within which returns the desired value. Or, more sensibly, define a reusable function that generates the required value and call upon it.