JavaScript对象数组拼接字符串:添加换行并以分号结尾的方法
问题
我有一个对象数组,需要将其中checked属性为true的对象的adsRole、adsLicense、adsDataConcept、adsManagedGeography、adsManagedSegment属性拼接为字符串,要求每个对象对应的拼接字符串以分号结尾,且各字符串之间添加换行。尝试在字符串末尾添加\n但未达到预期效果,当前使用的JavaScript代码及输出如下:
当前代码
let arr= [ { "adsFactId": "753648", "adsRole": "Authorized Redistributor (AR)", "adsLicense": "Target", "adsLicenseEffectiveDate": "04/17/2023", "adsLicenseExpiryDate": "", "adsDataConcept": "AlexProposed3 [P], Asset · L1, AlexProposed4 [P], AlexProposed2 [P], AlexProposed1 [P]", "adsManagedGeography": "North America · L2", "adsManagedSegment": "Institutional Clients Group [L3], Legacy Franchises [L3]", "adsStatus": "Active", "adsTechAsset": "Citi Building Controls Systems | 170794", "selectedIds": [ "753648" ], "checked": true, "checkBoxPatched": true }, { "adsFactId": "753337", "adsRole": "Authorized Redistributor (AR)", "adsLicense": "Interim", "adsLicenseEffectiveDate": "04/12/2023", "adsLicenseExpiryDate": "04/29/2023", "adsDataConcept": "Adsddsdsdsdd [P], AlexProposed1 [P]", "adsManagedGeography": "Unspecified", "adsManagedSegment": "Unspecified", "adsStatus": "Active", "adsTechAsset": "Citi Building Controls Systems | 170794", "selectedIds": [ "753648" ], "checked": true, "checkBoxPatched": true }, ] let newArr = arr.map(selectedObj => { if(selectedObj.checked) { return selectedObj.adsRole + ", " + selectedObj.adsLicense + ", " + selectedObj.adsDataConcept + ", " + selectedObj.adsManagedGeography + ", " + selectedObj.adsManagedSegment } }).filter((str) => str.length !== 0).join(';\n'); console.log(newArr)
当前输出
Authorized Redistributor (AR), Target, AlexProposed3 [P], Asset · L1, AlexProposed4 [P], AlexProposed2 [P], AlexProposed1 [P], North America · L2, Institutional Clients Group [L3], Legacy Franchises [L3]; Authorized Redistributor (AR), Interim, Adsddsdsdsdd [P], AlexProposed1 [P], Unspecified, Unspecified
解决方案
问题出在join(';\n')的逻辑:join仅在数组元素之间插入分隔符,最后一个元素不会自动添加分号。正确做法是在每个符合条件的对象拼接时直接加上分号,再用换行符连接结果。
修改后的代码
let arr= [ // 原数组内容保持不变 { "adsFactId": "753648", "adsRole": "Authorized Redistributor (AR)", "adsLicense": "Target", "adsLicenseEffectiveDate": "04/17/2023", "adsLicenseExpiryDate": "", "adsDataConcept": "AlexProposed3 [P], Asset · L1, AlexProposed4 [P], AlexProposed2 [P], AlexProposed1 [P]", "adsManagedGeography": "North America · L2", "adsManagedSegment": "Institutional Clients Group [L3], Legacy Franchises [L3]", "adsStatus": "Active", "adsTechAsset": "Citi Building Controls Systems | 170794", "selectedIds": [ "753648" ], "checked": true, "checkBoxPatched": true }, { "adsFactId": "753337", "adsRole": "Authorized Redistributor (AR)", "adsLicense": "Interim", "adsLicenseEffectiveDate": "04/12/2023", "adsLicenseExpiryDate": "04/29/2023", "adsDataConcept": "Adsddsdsdsdd [P], AlexProposed1 [P]", "adsManagedGeography": "Unspecified", "adsManagedSegment": "Unspecified", "adsStatus": "Active", "adsTechAsset": "Citi Building Controls Systems | 170794", "selectedIds": [ "753648" ], "checked": true, "checkBoxPatched": true }, ] let newArr = arr .filter(selectedObj => selectedObj.checked) // 先过滤有效对象,提升效率 .map(selectedObj => { // 每个对象的拼接结果直接追加分号 return `${selectedObj.adsRole}, ${selectedObj.adsLicense}, ${selectedObj.adsDataConcept}, ${selectedObj.adsManagedGeography}, ${selectedObj.adsManagedSegment};` }) .join('\n'); // 用换行符连接所有带分号的字符串 console.log(newArr)
修改后输出
Authorized Redistributor (AR), Target, AlexProposed3 [P], Asset · L1, AlexProposed4 [P], AlexProposed2 [P], AlexProposed1 [P], North America · L2, Institutional Clients Group [L3], Legacy Franchises [L3]; Authorized Redistributor (AR), Interim, Adsddsdsdsdd [P], AlexProposed1 [P], Unspecified, Unspecified;
关键调整说明
- 先过滤再映射:优先筛选
checked为true的对象,避免对无效对象做拼接操作,提升代码效率。 - 拼接时直接加分号:确保每个对象的结果字符串都以分号结尾,解决原代码最后一个元素无分号的问题。
- 用换行符连接结果:通过
join('\n')在每个带分号的字符串之间插入换行,实现预期的换行效果。
内容的提问来源于stack exchange,提问作者Mitul Panchal
相关产品推荐
相关产品推荐

