我试图将具有值的对象推送到Array
,但是我总是得到null
在控制台中不是对象。但是,当我console.log
对象被填充而不是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);
答案 0 :(得分: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
答案 1 :(得分:0)
您是否已初始化此文件。Lektionen?在推动对象之前尝试对其进行初始化。
this.Lektionen = [];
this.Lektionen.push(obj);
理想情况下,在声明变量的位置或要在数组中推送项目的函数中初始化它。