Function

Module: @ember/object
import { map } from '@ember/object/computed';
dependentKey
String
additionalDependentKeys
Array
optional array of additional dependent keys
callback
Function
returns
ComputedProperty
an array mapped via the callback

Returns an array mapped via 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.

1
function(item, index);

Example

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

let Hamster = EmberObject.extend({
  excitingChores: map('chores', function(chore, index) {
    return chore.toUpperCase() + '!';
  })
});

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

hamster.get('excitingChores'); // ['CLEAN!', 'WRITE MORE UNIT TESTS!']

You can optionally pass an array of additional dependent keys as the second parameter to the macro, if your map function relies on any external values:

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

let Hamster = EmberObject.extend({
  excitingChores: map('chores', ['shouldUpperCase'], function(chore, index) {
    if (this.shouldUpperCase) {
      return chore.toUpperCase() + '!';
    } else {
      return chore + '!';
    }
  })
});

let hamster = Hamster.create({
  shouldUpperCase: false,

  chores: ['clean', 'write more unit tests']
});

hamster.get('excitingChores'); // ['clean!', 'write more unit tests!']
hamster.set('shouldUpperCase', true);
hamster.get('excitingChores'); // ['CLEAN!', 'WRITE MORE UNIT TESTS!']