Activity › Forums › Salesforce® Discussions › How to use map in Salesforce lightning component?
Tagged: Aura Attribute, Aura Component, Aura Handlers, AuraEnabled, Map, Salesforce Lightning, Salesforce Lightning Components
-
How to use map in Salesforce lightning component?
Posted by Prachi on July 19, 2018 at 2:11 PMHow to use map in Salesforce lightning component?
Parul replied 7 years, 7 months ago 3 Members · 2 Replies -
2 Replies
-
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
} - [adinserter block='9']
-
<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
componentHope it helps
Log In to reply.