Create a “key: value” pair inside anonymous object if condition is true, otherwise do nothing [duplicate]

In perl, I can do this:

{ foo => 1, condition ? (bar => 2) : () }

The result will be either { foo => 1 } or { foo => 1, bar => 2 } depending on condition.

Is something like this possible with javascript objects?