I've encountered a problem where my array is getting remapped into an object, but it's excluding the 0 (zero) values. I need to include these zero values in the final object. How can I adjust my code to achieve this? Your help is much appreciated! :)
Here is my array:
var data = {
"queryInfo": {
"totalRows": "18"
},
"resultset": [
["A", "MAXI", "ACC - GC", "SER", 5646.5],
["A", "MAXI", "ACC - SACC", "KIT", 2474.93],
["A", "MAXI", "ACC - SACC", "NET", 5418.72],
["A+", "MAXI", "ACC - DIV", "FORM", 1531.04],
// more array elements...
],
"metadata": [{
"colIndex": 0,
"colType": "String",
"colName": "CP"
}, {
"colIndex": 1,
"colType": "String",
"colName": "ST"
}, {
// more metadata elements...
}]
};
and here is my function:
function recom(resultset) {
var obj = {};
for (var j = 0; j < resultset.length; j++) {
if (resultset[j]) {
obj[data.metadata[j].colName] = resultset[j];
}
}
return obj;
}
var arr = [];
for (var i = 0; i < data.resultset.length; i++) {
if (data.resultset[i] !== null) {
arr.push(recom(data.resultset[i]));
}
}
The issue occurs when a value of 0 in index 4 of `data.resultset` causes my function to skip that level in the final object. The correct output should retain the value as shown below:
Correct result: {"CP":"A+","ST":"MAXI","GR":"SCC","PR":"HEL","VALUE":0}
I want my function to work correctly even with 0 values included. Any suggestions on how to fix this would be highly appreciated.
Thank you for your assistance.