在O/P中,如果属性是空的,我会在字符串的中间和结尾看到, ,
,因为它们是用逗号分隔的。我怎样才能删除它,应该只有一个逗号,即使键是空的?
Exp O/P:授权再分销商(AR)、文档· L1、合规性· 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);
1条答案
按热度按时间mzillmmw1#
1.在
map()
之前使用filter()
来过滤掉selectedObj.checked
1.使用
filter()
和join()
跳过空值