【问题标题】:Access Array via Index通过索引访问数组
【发布时间】:2013-02-21 22:20:57
【问题描述】:

我尝试通过阅读 Todo-List 示例中如何处理标签以及如何处理各方示例中如何处理 RSVPS 来弄清楚这一点,但我无法找到实现目标的一般方法。 我有一个计划集合,其中包含名称 ownerid 和 excerciselist

Plans.insert({name: names[i], ownerId: 1, excerciselist: excercises});

现在在这个 Excerciselist 中,我想按 ID 添加一个未定义的 Excercises 数量。 我有一个 Excercisecollection 包含以下内容:

Excercises.insert({name:names[i],muscle:muscle[i],device:devices[i],description:descriptions[i]});

现在所有这些练习默认都有一个唯一的 _id 元素。 向 excerciselist 添加东西没问题,我可以通过 Push 做到这一点。 但我想不通的是,我如何只能通过它的索引访问 excerciselist 中的某些 ID。

我只能通过

访问整个 Stringarray 并以 HTML 格式输出

{{#每个平面数组}} {{练习表}} {{/每个}} 但是没有可能做类似的事情 {{ 练习者 }}

我也尝试过只将 excerciselist 返回到 planarray,但问题是因为它只是索引而不是映射,LiveHTML 无法访问它。

有没有人知道如何解决这个问题?

【问题讨论】:

    标签: javascript mongodb meteor


    【解决方案1】:

    为什么不将唯一 id 字段添加到 Excersies 插入中?

    Excercises.insert({ uniqueID: [i], name: names[i], muscle: muscles[i], device: devices[i], description: descriptions[i]});
    

    这样您就可以根据 uniqueID 字段获得所需的练习。

    哦,您可能应该将“uniqueID”称为更有意义的名称。

    【讨论】:

      【解决方案2】:

      我找到了一个小解决方法,它与我的想法不完全一样,但它可以完成工作。

        Template.editplan.excercises = function() {
          var names = [];
          var add = [];
          var adder = null;
          for(var i = 0; i < this.excerciselist.length; i++)
          {
            add[i] = [];
            adder = Excercises.find({_id: this.excerciselist[i]});
            add[i]['_id'] = this.excerciselist[i];
            add[i]['location'] = i;
            adder.forEach(function (obj) {
              add[i]['name'] = obj.name;
            });
            names.push(add[i]);
          }
          return names;
        };
      

      基本上我创建了一个新数组,在其中放置了我想要的数据,以便我可以在 LiveHTML 中读取它,请参见下面的示例

      {{#each planarray}}
              <h1>{{name}}</h1>
              {{#each excercises}}
              {{name}}
              {{/each}}
              <select name="selectedexcercise{{_id}}" id="selectedexcercise{{_id}}">
                  {{> excerciseoption}}
              </select>
              <input type="button" class="addtoplan" value="Eine Übung hinzfügen">
          {{/each}}
      

      但必须有更有效或更好的方法....至少我希望如此!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-26
        • 1970-01-01
        • 1970-01-01
        • 2011-12-24
        • 2021-12-04
        相关资源
        最近更新 更多