【问题标题】:Angular6/Typescript - Add Object into Array of another ArrayAngular6/Typescript - 将对象添加到另一个数组的数组中
【发布时间】:2018-09-13 11:53:34
【问题描述】:

我需要通过将 ID 写入文本字段来将对象推送到包含另一个数组的数组中。如何在数组中使用带有数组的方法?

演示:https://angular-flj24f.stackblitz.io

类:

 class Food {
    id: number;
    name: string;
    preis: number;
    art: string;
}

class Foodplan {
    id: number;
    foodPerWeek: number[] = new Array(5);

提前致谢。

【问题讨论】:

    标签: arrays angular typescript push


    【解决方案1】:

    在 stackblitz 中,foodPlan 数组似乎只有一个元素,其索引为 0。要添加到该元素中的食物列表,您可以执行以下操作:

    this.foodPlan[0].essenProWoche.push(id);
    

    this updated stackblitz。将来,如果您添加一个控件以将多个元素添加到 foodPlan 数组中,那么您将需要知道要向哪个元素添加食物并使用适当的索引。

    【讨论】:

      猜你喜欢
      • 2023-03-28
      • 2019-02-13
      • 2021-12-28
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 2022-01-16
      • 2022-08-13
      • 1970-01-01
      相关资源
      最近更新 更多