skip to Main Content

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

console.log(adsList);

2

Answers


  1. Try like this :

    const adsList = arr.map(selectedObj => {
        if (selectedObj.checked) {
            return Object.values(selectedObj).filter(value => ((typeof value === 'string' || value instanceof String) && value !== '')).join(', ');
        } else {
            return '';
        }
    }).filter((str) => str.length !== 0).join(';nn');
    
    Login or Signup to reply.
    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(';nn');
    
    console.log(adsList);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search