Activity Forums Salesforce® Discussions How to use map in Salesforce lightning component?

  • Anjali

    Member
    July 20, 2018 at 10:58 am

    Hello Prachi,

    We  use Map in Lightning Component to add a key and value pair using the syntax myMap.set('myNewKey', myNewValue)

    Example-

    var myMap = cmp.get("v.sectionLabels");
    myMap.set('c', 'label3');

    The following example retrieves data from a map.

    for (var key in myMap){
    // something
    }

     

  • Parul

    Member
    September 28, 2018 at 5:16 pm

    <aura:component controller="MyMapClass">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="myMap" type="Map" />
    {!v.myMap.key1} <br/>
    {!v.myMap.key2}
    </aura:component>
    ({
    doInit: function(component, event, helper) {

    var action = component.get('c.getMyMap');

    action.setCallback(this,function(response){
    alert(JSON.stringify(response.getReturnValue()));
    component.set('v.myMap',response.getReturnValue());
    });
    $A.enqueueAction(action);
    }
    })
    public class MyMapClass {
    @AuraEnabled
    public static map<String,String> getMyMap(){
    Map<String,String> Mymap = new Map<String,String>();
    Mymap.put('key1','apple');
    Mymap.put('key2','mango');
    return Mymap;
    }
    }
    here is a simple example to use map in lightning component and use @AuraEnabled in you apex class method
    component

     

    Hope it helps

Log In to reply.

Popular Salesforce Blogs

Steps to Connect Pardot to Salesforce

Steps to Connect Pardot to Salesforce

Blog in

Steps to Integrate Salesforce and Pardot: There are some steps to install Pardot in salesforce 1- Click on this link https://login.salesforce.com/?ec=302&startURL=%2Fpackaging%2FinstallPackage.apexp%3Fp0%3D04t1W000000U7P8(production) https://test.salesforce.com/?ec=302&startURL=%2Fpackaging%2FinstallPackage.apexp%3Fp0%3D04t1W000000U7P8 (sandbox) 2- After…

Popular Salesforce Videos