0

I want to iterate over an array in handlebar, and with the index of each element print one element in other json or array that i have defined.

something like this:

{{#each groups as |group index|}}
    {{#sticky-element top=41}}
       {{ad-component data=adGroup.[index].ad2 class="average-ad"}}
    {{/sticky-element}} 
{{/each}}

My component its something like this:

import Ember from 'ember';

export default Ember.Component.extend({
  adGroup: {
    '0': {"ad1": "div-gpt-ad-123-0", "ad2": "div-gpt-ad-123-4"},
    '1': {"ad1": "div-gpt-ad-123-1", "ad2": "div-gpt-ad-123-5"},
    '2': {"ad1": "div-gpt-ad-123-2", "ad2": "div-gpt-ad-123-3"}
  }
});

but pitifully it is not possible do this in handlebar.

Some people recommend me use computed properties, but how can I create a computed property for this mission?

I don't know if these last 2 days are not my lucky days or what's going on, because i think this is something simple, but I cannot with this, some help please?

1
  • what is groups, why do you loop through it? do you even need group? Because, for this you can loop through adGroup directly with each-in. Commented Sep 8, 2017 at 9:58

1 Answer 1

1

If you structure adGroup as and array and not an object

adGroup: [
  {"ad1": "div-gpt-ad-123-0", "ad2": "div-gpt-ad-123-4"},
  {"ad1": "div-gpt-ad-123-1", "ad2": "div-gpt-ad-123-5"},
  {"ad1": "div-gpt-ad-123-2", "ad2": "div-gpt-ad-123-3"}
]

You could use ember-composable-helpers

ember install ember-composable-helpers

and get the value like

{{#each groups as |group index|}}
  {{#sticky-element top=41}}
   {{ad-component data=(get (object-at index adGroup) 'ad1') class="small-ad"}}
  {{/sticky-element}}
{{/each}}
Sign up to request clarification or add additional context in comments.

1 Comment

If groups is adGroup, looping through the array could just be data=group.ad1 the index wouldn't be necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.