相对于车把中另一个数组的索引迭代一个数组

Iterate an array with respect to another array's index in handlebars

{{#each locationTable as |locationShifts index|}}
   {{#each get reasons index as |value|}}
     {{value}}
   {{/each}}
{{/each}}

我想做这样的事情。同时使用 2 个助手 "each" 和 "get"。我怎样才能实现这样的目标?

会是

{{#each (get reasons index) as |value|}}
  {{value}}
{{/each}}

但我不能 100% 确定 get 是否适用于数组的数字索引。

如果我在自己的代码库中解决这个问题,我会在 Octane 中编写一个 @tracked getter 函数或在 pre-octane Ember 中编写一个计算 属性将两个数组映射到新对象。在 IMO 中,将它们映射到模拟它们之间关系的对象中要好得多(而不是 依赖 在代码库中各处的两个单独数组中的每个项目的索引,这不是我能找到的直观且似乎可能会在以后导致意外错误的东西)。

假设我有一个姓名列表和一个角色列表。

const names = ['Will', 'Wes', 'Janine'],
      roles = ['Father', 'Son', 'Mother'];

const people = names.map((name, idx) => {
  return {
    name: name,
    role: roles[idx]
  };
});

您可以推断出 computed/tracked 属性 的外观。关键是我已经设法对数据进行逻辑分组。这样在模板中你可以:

{{#each people as |person|}}
  {{person.name}} is a {{person.role}}
{{/each}}

而不是需要同时走到离散阵列。在 javascript 中为您的数据建模,模板很容易 write/follow