Function
uniqBy (dependentKey, propertyKey) ComputedProperty public
Module: | @ember/object |
---|
Defined in packages/@ember/object/lib/computed/reduce_computed_macros.js:850
import { uniqBy } from '@ember/object/computed';
- 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:
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:
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' }]
© 2020 Yehuda Katz, Tom Dale and Ember.js contributors
Licensed under the MIT License.
https://api.emberjs.com/ember/3.25/functions/@ember%2Fobject%2Fcomputed/uniqBy