In these examples, cmp is a reference to a component in your JavaScript code. It’s usually easy to get a reference to a component in JavaScript code. Remember that the body attribute is an array of components, so you can use the JavaScript Array methods on it.
To replace the current value of a component’s body with another component:
// newCmp is a reference to another component cmp.set("v.body", newCmp);
To clear or empty the current value of a component’s body:
cmp.set("v.body", []);
To append a newCmp component to a component’s body:
var body = cmp.get("v.body"); // newCmp is a reference to another component body.push(newCmp); cmp.set("v.body", body);
To prepend a newCmp component to a component’s body:
var body = cmp.get("v.body"); body.unshift(newCmp); cmp.set("v.body", body);
To remove an indexed entry from a component’s body:
var body = cmp.get("v.body"); // Index (3) is zero-based so remove the fourth component in the body body.splice(3, 1); cmp.set("v.body", body);