My project makes use of Dev Extreme and it's datagrid component. This datagrid displays data to my users where they can select individual rows on the datagrid and pass them to my backend via an API call.
In the API call, I need to pass over an array of values that I'm collecting from whichever row has been selected. When I output my array to the console, everything is how I expect it to be. Yet, when I place it in JSON.stringify()
the array is empty.
Why does this happen? As I'm expecting the result to look like: { 'LoadContextIds': [1,2,3], "ReportingCobid": "20190119"}
.
My logic for the above is as follows:
function runBatchWithLoadContexts() {
var data = [];
getDataGridInstance().getSelectedRowsData().done(function (rowData) {
for (var i = 0; i < rowData.length; i++) {
data.push(rowData[i].LoadContextId);
}
});
var obj = { 'LoadContextIds': data, "ReportingCobid": cobDate };
console.log(data);
console.log(JSON.stringify(obj));
}
And attached is a snippet from my console.log showing the above results as well. The top line is the output format of `data' and the bottom is the result of stringifying it.
All that @junvar said is correct and if you create your obj inside the done function after the for loop, all the data is going to be there.
function runBatchWithLoadContexts() { var data = [];
getDataGridInstance().getSelectedRowsData().done(function (rowData) {
for (var i = 0; i < rowData.length; i++) {
data.push(rowData[i].LoadContextId);
}
let obj = { 'LoadContextIds': data, "ReportingCobid": cobDate };
console.log(data);
console.log(JSON.stringify(obj));
});
}