Function

filter (dependentKey, callback) Ember.ComputedProperty public

Module: @ember/object
import { filter } from '@ember/object/computed';
dependentKey
String
callback
Function
returns
Ember.ComputedProperty
the filtered array

Filters the array by the callback.

The callback method you provide should have the following signature. item is the current item in the iteration. index is the integer index of the current item in the iteration. array is the dependant array itself.

function(item, index, array);
let Hamster = Ember.Object.extend({
  remainingChores: Ember.computed.filter('chores', function(chore, index, array) {
    return !chore.done;
  })
});

let hamster = Hamster.create({
  chores: [
    { name: 'cook', done: true },
    { name: 'clean', done: true },
    { name: 'write more unit tests', done: false }
  ]
});

hamster.get('remainingChores'); // [{name: 'write more unit tests', done: false}]

You can also use @each.property in your dependent key, the callback will still use the underlying array:

let Hamster = Ember.Object.extend({
  remainingChores: Ember.computed.filter('[email protected]', function(chore, index, array) {
    return !chore.get('done');
  })
});

let hamster = Hamster.create({
  chores: Ember.A([
    Ember.Object.create({ name: 'cook', done: true }),
    Ember.Object.create({ name: 'clean', done: true }),
    Ember.Object.create({ name: 'write more unit tests', done: false })
  ])
});
hamster.get('remainingChores'); // [{name: 'write more unit tests', done: false}]
hamster.get('chores').objectAt(2).set('done', true);
hamster.get('remainingChores'); // []

© 2020 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://api.emberjs.com/ember/2.18/functions/@ember%2Fobject%2Fcomputed/filter