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

How can I remove comma from string if value is empty?

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(';\n\n');

console.log(adsList);

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 :

  1. Use filter() before map() to filter out selectedObj.checked
  2. Use filter() and join() to skip empty values
const arr = [{id:"324101",role:"Authorized Redistributor (AR)",license:"Target",dataConcept:"Agreement \xb7 L1, Asset \xb7 L1, Account \xb7 L1",managedGeography:"International \xb7 L2",managedSegment:"Core Citi Businesses [L2]",enterpriseProduct:"Borrowing Products \xb7 L2"},{id:"324230",role:"Authorized Redistributor (AR)",license:"",dataConcept:"Document \xb7 L1, Compliance \xb7 L1",managedGeography:"",managedSegment:"",enterpriseProduct:"",checked:!0,checkBoxPatched:!0},{id:"324383",role:"System Of Record (SOR)",license:"Target",dataConcept:"Market \xb7 L1, Holding \xb7 L1",managedGeography:"",managedSegment:"",enterpriseProduct:""},{id:"324410",role:"System Of Record (SOR)",license:"Interim",dataConcept:"Holding \xb7 L1, Party \xb7 L1, Balance \xb7 L1, Account \xb7 L1, Compliance \xb7 L1",managedGeography:"",managedSegment:"Corporate / Other [L2]",enterpriseProduct:"Borrowing Products \xb7 L2, Fee-Based Products \xb7 L2, Lending Products \xb7 L2, Issued Monetary Instruments \xb7 L2, Traded Loans \xb7 L2, Deposit Products \xb7 L2, Treasury Management \xb7 L2"},{id:"364769",role:"System Of Record (SOR)",license:"Interim",dataConcept:"Asset \xb7 L1, Account \xb7 L1",managedGeography:"Total Citi Geography \xb7 L1",managedSegment:"Total Citi [L1]",enterpriseProduct:""}];

const adsList = arr.filter(selectedObj => selectedObj.checked).map(selectedObj => {
  return [selectedObj.role, selectedObj.license, selectedObj.dataConcept, selectedObj.managedGeography, selectedObj.managedSegment].filter(s => s.trim()).join(', ')
}).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