title: How to dynamically add attributes to objects in javascript?
body: <p>As we all know, we can use if else to add an attribute to object like this:</p>
  <pre><code>if(key){
    obj[key] = value
  }
  </code></pre>
  <p>but I have a lot of keys.</p>
  <p>I know I can get this done like this:</p>
  <pre><code>keyValues.forEach(({key,value})=&gt;{
  if(key){
    obj[key] = value
  }
  }
  </code></pre>
  <p>but i want to know can i do this in another way like:</p>
  <pre><code>obj={
   [this.key1?'key1':undefined]:this.key1,
   [this.key2?'key2':undefined]:this.key2,
  }
  </code></pre>
  <p>in this way,the object will get an attribute which key is undefined... how can i do to remove undefined attribute and keep the code style in the second way.</p>