function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lukesh KarmoreLukesh Karmore 

How to add columns dynamically in js file of aura component for lightning:datatable??

I have component where I can select field values from multiselect picklist and this field value should map automatically in column of datatable. I tried below 
 
 var fieldNames=component.get("v.fieldslabel");//list of selected fields from picklist         console.log('****** '+fieldNames);   
      for (var i=0; i < fieldNames.length; i++) {
             var column=[                 
{label: fieldNames[i],fieldName:   fieldNames[i]  }     
        ]      
   }       
component.set('v.columns', this.column);
can any one give me idea.Thanks ! User-added imagevalue is visible  when I inspect , but not in component.
SwethaSwetha (Salesforce Developers) 
HI Lukesh,
A similar ask was posted here: https://salesforce.stackexchange.com/questions/384098/how-can-i-make-lightningdatatable-columns-dynamic according to which

"If you have a dual list box, presumably your data is already in the form of:
[ { label: 'Option 1', value: 'Option1' }, ... ]
Which means you'd want to use the appropriate properties:
var columnsDataTable = selectedDimensions.map((option) => ({ 
  label: option.label, 
  fieldName: option.label,
  type: 'string'
}));
"
Thanks