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?
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?
Share Improve this question asked Feb 1, 2016 at 16:16 J21042J21042 1,2844 gold badges19 silver badges34 bronze badges4 Answers
Reset to default 3Try:
columnDefs.forEach(function(obj) {
obj.cellClass = myFunction;
});
You might be misguided about what in
does: it already iterates the key of an object (or the indexes of an array).
What you want instead is to parse your json string once, alter it in a loop, then re-serialize it:
var json = "...."; // your json input
var columnDefs = JSON.parse(json);
for (var i in columnDefs) { // columnDefs is an array, i the index
var row = columnDefs[i]; // index into columnDefs to get the object
row.cellClass = myFn(row);
}
var newJson = JSON.stringify(columnDefs); // your json output – skip this step if not needed
Is map what you need? Map returns a new array based on transforming the old one.
var arr = [
{ name: 'Report', field: 'RPT', width: 80 },
{ name: 'Element', field: 'ELEM', width: 200 }
].map(function(element) {
element.cellClass = myFunction;
return element;
});
var keys = Object.keys( columnDefs );
keys.forEach(function(item){
var col = columnDefs[item]
col['cellClass'] = myFunction
}