So say you have a any array of objects in your ember app
testArray = [{name: "bob the builder"}, {name: "sarah the smasher"}]
> [Object, Object]
Did you know that there is a ‘magic’ keyword called @each
you can use to pluck properties from all your objects like this:
testArray.get('@each.name')
> v {_keyName: "name", _owner: v, _content: Array[2], __nextSuper: undefined, __ember1448307773263: null…}
It returns a weird ember object array thing that you can convert into a normal array with toArray()
like so:
testArray.get('@each.name').toArray()
> ["bob the builder", "sarah the smasher"]
Normally, I see this combined with a map
when the values need to be processed in some way
testArray.get('@each.name').toArray().map (names)-> name.replace(" ", "")
> ["bobthe builder", "sarahthe smasher"]
But did you know that ember has an invoke function that will call a function with any params you want on each element?
testArray.get('@each.name').invoke('replace', " ", "")
> ["bobthe builder", "sarahthe smasher"]
Much more succinct than how it’s normally done :)