title: Creating a javascript object with an optional key
body: <p>I'm looking for an idiomatic method of creating an object with an optional key, i.e. the shortest way of doing the following:</p>
  
  <pre><code>let obj = {a: 1}
  if(condition)
      obj['b'] = 2
  console.log(obj)
  </code></pre>
  
  <p>One way I've thought of is:</p>
  
  <pre><code>console.log({a: 1, ...(condition ? {b: 2} : {})})
  </code></pre>
  
  <p>But is there a better way?</p>