In this article we are going to have look at some methods of Ember.computed class which helps you to refactor code or write more readable code. And most importantly if ember provides its own methods then why to write code with number of lines to do same thing.

Ember.computed class documentation: Ember.computed

getEach: This method returns all the values for attributes passed as argument.

    var names = []
    this.get('model').forEach((user)=>{
      names.push(user.get('name'));
    });

This can be simply done as:

    this.get('model').getEach('name');

It will return array of names.

sum: This method performs sum of all array elements as name suggests:

    var values = [1, 2, 3]
    var sum = 0
    values.forEach((val) => {
      sum = sum + val;
    });

This can be simply done as:

    sum: Ember.computed.sum('values')

filterBy : It returns all records with matching attribute value.

    publishedPosts: Ember.computed('posts.@each.isPublished', function () {
      var posts = this.get('posts');
      return posts.filterBy('isPublished', true)
    });

This can be simply done as:

    Ember.computed.filterBy('posts', 'isPublished', true);

min : It returns min value from array as name suggests

    minNumber: Ember.computed.min('someArray')

Same way we can get max number from array with max method.

    maxNumber: Ember.computed.max('someArray')

There are other methods as well which are helpful and nicely explained in documentation. Main purpose behind this article is to highlight this class. Hope this helps.