Function

Module: @ember/object
dependentKey
String
propertyKey
String
returns
ComputedProperty
an array mapped to the specified key

Returns an array mapped to the specified key.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { set } from '@ember/object';
import { mapBy } from '@ember/object/computed';

class Person {
  children = [];
  @mapBy('children', 'age') childAges;
}

let lordByron = new Person();

lordByron.childAges; // []

set(lordByron, 'children', [
  {
    name: 'Augusta Ada Byron',
    age: 7
  }
]);
lordByron.childAges; // [7]

set(lordByron, 'children', [
  ...lordByron.children,
  {
    name: 'Allegra Byron',
    age: 5
  }, {
    name: 'Elizabeth Medora Leigh',
    age: 8
  }
]);
lordByron.childAges; // [7, 5, 8]

Classic Class Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import EmberObject, { set } from '@ember/object';
import { mapBy } from '@ember/object/computed';

let Person = EmberObject.extend({
  childAges: mapBy('children', 'age')
});

let lordByron = Person.create({ children: [] });

lordByron.childAges; // []

set(lordByron, 'children', [
  {
    name: 'Augusta Ada Byron',
    age: 7
  }
]);
lordByron.childAges; // [7]

set(lordByron, 'children', [
  ...lordByron.children,
  {
    name: 'Allegra Byron',
    age: 5
  }, {
    name: 'Elizabeth Medora Leigh',
    age: 8
  }
]);
lordByron.childAges; // [7, 5, 8]