In Javascript, how to conditionally add a member to an object?
<p>I would like to create an object with a member added conditionally.
The simple approach is:</p>

<pre><code>var a = {};
if (someCondition)
    a.b = 5;
</code></pre>

<p>Now, I would like to write a more idiomatic code. I am trying:</p>

<pre><code>a = {
    b: (someCondition? 5 : undefined)
};
</code></pre>

<p>But now, <code>b</code> is a member of <code>a</code> whose value is <code>undefined</code>. This is not the desired result.</p>

<p>Is there a handy solution?</p>

<p><strong>Update</strong></p>

<p>I seek for a solution that could handle the general case with several members.</p>

<pre><code>a = {
  b: (conditionB? 5 : undefined),
  c: (conditionC? 5 : undefined),
  d: (conditionD? 5 : undefined),
  e: (conditionE? 5 : undefined),
  f: (conditionF? 5 : undefined),
  g: (conditionG? 5 : undefined),
 };
</code></pre>
