Theodore R. Smith
Theodore R. Smith

Reputation: 23231

Accessing a multidimensional array from a parent each's id in Handlebars

I have the data:

priceGroups:
[
  {
    label: 'rate1",
    price: 1.00,
    numPeople: 1
  },
  {
    label: 'rate 2",
    price: 2.00,
    numPeople: 2
  },
  {
    label: 'rate 3",
    price: 15.00,
    numPeople: 4
  },
];

and

labels:
[
  'Solo',
  'Duo',
  'Trio'
  '4 People',
  '5 People
];

I want to be able to do this:

{{#priceGroups}}
   {{for (a = 1; a <= {{this.numPeople}}; ++a)}}
      <th>{{label[a]}}</th>
   {{/for}}
{{/priceGroups}}

I have tried so much and nothing works.

Please help. It is important.

Upvotes: 0

Views: 808

Answers (1)

jfriend00
jfriend00

Reputation: 707198

I think you can just use this:

{{#each priceGroups}}<th>{{label}}</th>{{/each}}

The {{#each priceGroups}} gets you an iteration of the priceGroups array and then each item of the array (which is an object) is presented to the template one after another so you can then reference the {{label}} property in that object.

You don't embed javascript directly into handlerbars templates like you're trying to do.

Upvotes: 2

Related Questions