Newer Version Available
Collection Types
Here are the supported collection type values.
| type | Example | Description |
|---|---|---|
| type[] (Array) | <aura:attribute name="colorPalette" type="String[]" default="['red', 'green', 'blue']" /> | An array of items of a defined type. |
| List | <aura:attribute name="colorPalette" type="List" default="['red', 'green', 'blue']" /> | An ordered collection of items. |
| Map | <aura:attribute name="sectionLabels" type="Map" default="{ a: 'label1', b: 'label2' }" /> | A collection that maps keys to values. A map can’t contain duplicate keys. Each key can map to at most one value. Defaults to an empty object, {}. Retrieve values by using cmp.get("v.sectionLabels")['a']. |
| Set | <aura:attribute name="collection" type="Set" default="['red', 'green', 'blue']" /> | A collection that contains no duplicate elements. The order for set items is not guaranteed. For example, "red,green,blue" might be returned as "blue,green,red". |
Setting List Items
There are several ways to set items in a list. To use a client-side controller, create an attribute of type List and set the items using component.set().
This example retrieves a list of numbers from a client-side controller when a button is clicked.
1<aura:attribute name="numbers" type="List"/>
2<ui:button press="{!c.getNumbers}" label="Display Numbers" />
3<aura:iteration var="num" items="{!v.numbers}">
4 {!num.value}
5</aura:iteration>1/** Client-side Controller **/
2({
3 getNumbers: function(component, event, helper) {
4 var numbers = [];
5 for (var i = 0; i < 20; i++) {
6 numbers.push({
7 value: i
8 });
9 }
10 component.set("v.numbers", numbers);
11 }
12})To retrieve list data from a controller, use aura:iteration.
Setting Map Items
To add a key and value pair to a map, use the syntax myMap['myNewKey'] =
myNewValue.
The
following example retrieves data from a map.
1var myMap = cmp.get("v.sectionLabels");
2 myMap['c'] = 'label3';1for (key in myMap){
2 //do something
3}