How to append an object to a Javascript JSON object
I have a javascript object, which contains JSON in the form of an array of objects, such as:
columnDefs: [
{ name: 'Report', field: 'RPT', width: 80 },
{ name: 'Element', field: 'ELEM', width: 200 },
...
]
I need to add
cellClass: myFunction
to each individual column def in the columnDefs array of objects, so it is like:
columnDefs: [
{ name: 'Report', field: 'RPT', width: 80, cellClass: myFunction },
{ name: 'Element', field: 'ELEM', width: 200, cellClass: myFunction },
...
]
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:
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;
}
However the above code generates the following which is not what is needed:
[ { name: 'Report', field: 'RPT', width: 80 }, { cellClass: myFunction } ]
There must be an easier and better way to do this?