Ember.Enumerable no group by?

I was looking for one the other day and just ended up doing this. In this case I am grouping by the type property and it’s content is pushed to the contents array. It’s not very optimal, but it works in my use-case. I stuck this on my array controller but it can be applied to anything really.

groupedResults: function () {
   var result = [];

   this.get('content').forEach(function(item) {
      var hasType = result.findBy('type', item.get('type');

      if(!hasType) {
         result.pushObject(Ember.Object.create({
            type: item.get('type'),
            contents: []
         }));
      }

      result.findBy('type', item.get('type')).get('contents').pushObject(item);
   });

   return result;
}.property('content.[]')
5 Likes