Skip to content

Instantly share code, notes, and snippets.

@eoinkelly
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eoinkelly/10951933 to your computer and use it in GitHub Desktop.
Save eoinkelly/10951933 to your computer and use it in GitHub Desktop.
Iterating over a collection with access to the index in Ember

Iterating over a collection with access to the index in Ember

// within your view/component
collectionWithIndices: function () {
  return this.get('collection').map(function (item, idx) {
    item.index = String(idx); // we need 0 to be "0" or Handlebars will consider it falsy
    return item;
  });
}.property('collection.@each'),
{{!-- your template --}}
{{#each thing in collectionWithIndices}}
 index: {{thing.index}}
{{/each}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment