I am currently working with an array object that contains similar values. I have been able to successfully filter out the duplicate values using a loop and then add the unique values to another object called objectProperties. Everything seems to be working fine, except for the fact that I am getting NULL values inside the category property.
// Here is the data I'm extracting
var data = [
{
"label":"May 14",
"value":56714.4,
"proID":"ISB"
},
{...} - additional data entries ...
];
var propertiesObject = { // This is my object
type: 'mscolumn2d',
renderAt: 'chart-container',
width: '1000',
height: '500',
dataFormat: 'json',
dataSource: {
chart: {
caption: "Kilos per Date Comparison"
},
categories: [
{
category: []
}
]
}
};
var propCount = Object.keys(data).length;
var checkSameLabel = data[0].label;
var firstIndex = {"label":data[0].label};
propertiesObject.dataSource.categories[0].category[0] = firstIndex;
var currentProject = data[0].proID, counterCurrentProject = 0;
for(let i = 0; i < propCount; i++) {
if(checkSameLabel !== data[i].label) {
const value = data[i].label;
var obj = {
"label": value
};
propertiesObject.dataSource.categories[0].category[i] = value;
}
checkSameLabel = data[i].label;
}
console.log(JSON.stringify(propertiesObject));
document.getElementById("result").innerHTML = JSON.stringify(propertiesObject);
<div id="result"></div>
The expected output should look like this inside the "category" section:
{ "label": "May 14" },
{ "label": "May 15" },
{ "label": "May 16" },
{ "label": "May 17" }
However, I seem to be encountering some issues with NULL values due to my loop implementation or possibly some other mistake in the code.