In the O/P, if the properties are empty I see , ,
in the middle and in the end of the string since they are separated by comma. How can I remove it, there should only be single comma even if keys are empty?
Exp O/P: 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(';nn');
console.log(adsList);
2
Answers
Try like this :
filter()
beforemap()
to filter outselectedObj.checked
filter()
andjoin()
to skip empty values