Function

Module: @ember/object
dependentKey
String
propertyKey
String
returns
ComputedProperty
computes a new array with all the unique elements from the dependent array

A computed property which returns a new array with all the unique elements from an array, with uniqueness determined by specific key.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import { set } from '@ember/object';
import { uniqBy } from '@ember/object/computed';

class Hamster {
  constructor(fruits) {
    set(this, 'fruits', fruits);
  }

  @uniqBy('fruits', 'id') uniqueFruits;
}

let hamster = new Hamster([
  { id: 1, 'banana' },
  { id: 2, 'grape' },
  { id: 3, 'peach' },
  { id: 1, 'banana' }
]);

hamster.uniqueFruits; // [ { id: 1, 'banana' }, { id: 2, 'grape' }, { id: 3, 'peach' }]

Classic Class Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import EmberObject from '@ember/object';
import { uniqBy } from '@ember/object/computed';

let Hamster = EmberObject.extend({
  uniqueFruits: uniqBy('fruits', 'id')
});

let hamster = Hamster.create({
  fruits: [
    { id: 1, 'banana' },
    { id: 2, 'grape' },
    { id: 3, 'peach' },
    { id: 1, 'banana' }
  ]
});

hamster.uniqueFruits; // [ { id: 1, 'banana' }, { id: 2, 'grape' }, { id: 3, 'peach' }]