Ionic / Angular:将对象推入数组对象

3

我试图将一个有值的对象推送到一个Array,但是在控制台中总是得到null不是一个对象。但是当我打印出这个对象时,它是填充的而不是null,那么为什么我不能将它推入到数组中呢?

            let obj: Lektion;
            obj = {
                LektionID: data2.string[0],
                Date: data2.string[1],
                StudentID: data2.string[2],
                Name: data2.string[3],
                Status: (data2.string[4] !== '0'),
                TeacherID: data2.string[5]
            };
            console.log(obj);
            this.Lektionen.push(obj);

输出

2个回答

0

我认为你对变量和接口感到困惑了。 Lektion 是一个接口,定义为变量并尝试将其推入其中。

   let Lektionen = [];
    const data2= {string:[1,2,3,4,5,6]};

    let obj:Lektion = {
                LektionID: data2.string[0],
                Date: data2.string[1],
                StudentID: data2.string[2],
                Name: data2.string[3],
                Status: (data2.string[4] !== '0'),
                TeacherID: data2.string[5]
            };
            this.Lektionen.push(obj);

            console.log(Lektionen); //will print the object

0

你有初始化这个 Lektionen 吗?尝试在推送对象之前进行初始化。

this.Lektionen = [];
this.Lektionen.push(obj);

最好在声明变量的地方或将项目推入数组的函数中初始化它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接