在数组Angular 2中添加动态值

时间:2017-03-07 14:01:11

标签: arrays algorithm

如何动态地在数组中添加值,我试过这样:

this.socios.forEach((data) => {
            this.fieldsSocios = [ 
        new ArraySocios(data.nome, data.participacao)

  ];

但是,在fieldsSocios中只有最后添加的值。 实际上应该是fieldSocios内的3个对象。

1 个答案:

答案 0 :(得分:0)

你是在每次迭代中初始化数组,this.fieldsSocios应该在循环开始之前初始化。

this.fieldsSocios = [];
this.socios.forEach((data) => {
 this.fieldsSocios.push(new ArraySocios(data.nome, data.participacao));
});