Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript remove semicolon from last string

const arr = [
    {
        "id": "753311",
        "role": "System Of Record (SOR)",
        "license": "Target",
        "DC": "Client · L2 (Inactive), Account · L1",
        "managedGeography": "North America · L2, International · L2",
        "managedSegment": "Institutional Clients Group [L3], Discontinued Ops [L2]",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": "752872",
        "role": "Authorized Redistributor (AR)",
        "license": "Interim",
        "DC": "Holding · L1, Document · L1, Compliance · L1",
        "managedGeography": "Unspecified",
        "managedSegment": "Unspecified",
        "checked": true,
        "checkBoxPatched": true
    },
    {
        "id": "752583",
        "role": "Authorized Redistributor (AR)",
        "license": "Target",
        "DC": "Agreement · L1, Asset · L1, Activity · L1, Account · L1",
        "managedGeography": "Unspecified",
        "managedSegment": "Unspecified"
    }
]

let adsList = arr.map(selectedObj => {
        if (selectedObj.checked) {
          return selectedObj.role + ", " + selectedObj.license + ", " + selectedObj.DC + ", " + selectedObj.managedGeography + ", " + selectedObj.managedSegment + ";\n"
        } else {
          return '';
        }
      }).filter((str) => str.length !== 0).join('\n');
      
 console.log(adsList)     

Hi there I’ve an array and I’m basically returning the string if object contains checked property and separating them with semicolon, but need to remove for the last one.

I’m not sure how do I remove the last semicolon after unspecified from the output in this case, any leads would be quite helpful.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Don’t add the semi-colon in the map call, but use it as separator in the final join call.

Not an issue, but:

  • you can use Boolean as filter callback function.
  • Instead of returning '', you can return .checked when it is falsy, which allows you to return one expression without if..else.
  • Maybe use a template string for concatenating the properties with commas.
const arr = [{"id": "753311","role": "System Of Record (SOR)","license": "Target","DC": "Client · L2 (Inactive), Account · L1","managedGeography": "North America · L2, International · L2","managedSegment": "Institutional Clients Group [L3], Discontinued Ops [L2]","checked": true,"checkBoxPatched": true},{"id": "752872","role": "Authorized Redistributor (AR)","license": "Interim","DC": "Holding · L1, Document · L1, Compliance · L1","managedGeography": "Unspecified","managedSegment": "Unspecified","checked": true,"checkBoxPatched": true},{"id": "752583","role": "Authorized Redistributor (AR)","license": "Target","DC": "Agreement · L1, Asset · L1, Activity · L1, Account · L1","managedGeography": "Unspecified", "managedSegment": "Unspecified"}];

let adsList = arr.map(obj =>
    obj.checked && `${obj.role}, ${obj.license}, ${obj.DC}, ${obj.managedGeography}, ${obj.managedSegment}`
).filter(Boolean).join(';\n\n');
      
 console.log(adsList)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading