title: How to append an object to a Javascript JSON object
body: <p>How to append an object to a Javascript JSON object</p>
  
  <p>I have a javascript object, which contains JSON in the form  of an array of objects, such as:</p>
  
  <pre><code>columnDefs: [
    { name: 'Report', field: 'RPT', width: 80 },
    { name: 'Element', field: 'ELEM', width: 200 },
    ...
  ]
  </code></pre>
  
  <p>I need to add </p>
  
  <pre><code>cellClass: myFunction 
  </code></pre>
  
  <p>to each individual column def in the columnDefs array of objects, so it is like:</p>
  
  <pre><code>columnDefs: [
    { name: 'Report', field: 'RPT', width: 80, cellClass: myFunction },
    { name: 'Element', field: 'ELEM', width: 200, cellClass: myFunction  },
    ...
  ]
  </code></pre>
  
  <p>Note that myFunction should not be encased in quotes. Since each column def is an object, and not an array, I understand that I need to convert it to an array before I can add anything to it:</p>
  
  <pre><code>var keys = Object.keys( columnDefs );
  for( var key in keys ) {
    var col = columnDefs[key]
    var colArray = [];
    colArray.push( JSON.parse( JSON.stringify( col )));
    colArray.push( { cellClass: myFunction } );
    colDefs[key] = colArray;
  }
  </code></pre>
  
  <p>However the above code generates the following which is not what is needed:</p>
  
  <pre><code>[    { name: 'Report', field: 'RPT', width: 80 },  { cellClass: myFunction } ]
  </code></pre>
  
  <p>There must be an easier and better way to do this?</p>