When the properties are empty in the output, I notice a double comma (,,) in the middle and at the end of the string due to being separated by commas. How can I remove this so that there is only a single comma even when keys are empty?
Expected Output: Authorized Redistributor (AR), Document · L1, Compliance · L1
const arr = [{
"id": "324101",
"role": "Authorized Redistributor (AR)",
"license": "Target",
"dataConcept": "Agreement · L1, Asset · L1, Account · L1",
"managedGeography": "International · L2",
"managedSegment": "Core Citi Businesses [L2]",
"enterpriseProduct": "Borrowing Products · L2"
},
{
"id": "324230",
"role": "Authorized Redistributor (AR)",
"license": "",
"dataConcept": "Document · L1, Compliance · L1",
"managedGeography": "",
"managedSegment": "",
"enterpriseProduct": "",
"checked": true,
"checkBoxPatched": true
}
]
const adsList = arr.map(selectedObj => {
if (selectedObj.checked) {
return selectedObj.role + ", " + selectedObj.license + ", " + selectedObj.dataConcept + ", " + selectedObj.managedGeography + ", " + selectedObj.managedSegment
} else {
return '';
}
}).filter((str) => str.length !== 0).join(';\n\n');
console.log(adsList);