Use Ember Get For Nested Objects
08 Feb 2017 - Help improve this postSometimes you have to get a variable from a nested object like this
const toys = {
muppets: {
ernie: {
head: 'wide'
}
}
}
If you are not sure if the complete object exist, you will have to do something like this:
const ernieHead = (toys && toys.muppets && toys.muppets.ernie && toys.muppets.ernie.head) ? toys.muppets.ernie.head : undefined;
// Do more with ernieHead...
With Ember you can use the get
-function:
const ernieHead = get(toys, 'toys.muppets.ernie.head');
So in a controller it would be like this:
import Ember from 'ember';
const { Controller, get } = Ember;
export default Controller.extend({
getToys() {
const ernieHead = get(toys, 'toys.muppets.ernie.head');
// Do more with ernieHead...
}
});
Happy coding! – Found a mistake or a typo? Please submit a PR to my GitHub-repo.