1

Hi there I've an array of object which from which I'm returning new string by concatenating

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

I need to add line breaks between concatenated string like above output, how can i achieve this. I tried by adding \n at the end on string but it didn't work

Mitul Panchal
  • 143
  • 2
  • 13
  • 3
    "the end" is where are the semicolon? If yes just add `.join(';\n');` and must work. – Simone Rossaini Apr 19 '23 at 09:30
  • 1
    @SimoneRossaini updated the answer it didn't work – Mitul Panchal Apr 19 '23 at 09:34
  • 2
    Work https://ibb.co/cNN7N6J ? – Simone Rossaini Apr 19 '23 at 09:36
  • 1
    I'm unable to open this link, can you plz provide the details in this comment box – Mitul Panchal Apr 19 '23 at 09:40
  • 6
    It is 'working' in your snippet. You'll need to add details if you want help. Are you putting the final string straight into an element? HTML doesn't respect newlines unless specified. Use a [`
    `](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre) element or use `white-space: pre-line` or `white-space: pre-wrap` in CSS. see: [Line break in HTML with '\n'](https://stackoverflow.com/questions/39325414/line-break-in-html-with-n)
    – pilchard Apr 19 '23 at 09:40
  • change the statement to this ```.filter((str) => str.length !== 0).toString().join(';
    ');```
    – Newbee Apr 19 '23 at 09:54
  • @Newbee it depends on where the OP is using the string. Voting to close for clarity/details. – pilchard Apr 19 '23 at 09:55
  • @pilchard Thanks `white-space: pre-line` does the trick, actually its not clearly visible here. – Mitul Panchal Apr 19 '23 at 09:57

0 Answers0