These common patterns are useful for working with attribute values in JavaScript.
component.get(String key) and component.set(String key, Object value) retrieves and assigns values associated with the specified key on the component. Keys are passed in as an expression, which represents an attribute value.
To retrieve an attribute value of a component reference, use component.find("cmpId").get("v.value").
Similarly, to set the attribute value of a component reference, use component.find("cmpId").set("v.value", myValue).
This example shows how you can retrieve and set attribute values on a component reference, represented by the button with an ID of button1.
In the following examples, cmp is a reference to a component in your JavaScript code.
To get the value of a component’s label attribute:
1var label = cmp.get("v.label");
Set an Attribute Value
To set the value of a component’s label attribute:
1cmp.set("v.label","This is a label");
Deep Set an Attribute Value
If an attribute has an object or collection type, such as Map, you can deep set properties in the attribute value using the dot notation for expressions. For example, this code sets a value for the firstName property in the user attribute.
1component.set("v.user.firstName", "Nina");
For deeply nested objects and attributes, continue adding dots to traverse the structure and access the nested values.
Let’s look at a component with a user attribute of type Map.