我想在数组中的特定索引处推送布尔值。每当我跑的代码,我得到这个错误,错误类型错误:未定义在数组中的特定索引处推送布尔值
无法读取属性“推”这是我的代码:
var check_ins = [];
for (var i = 0; i < this.employees.length; i++) {
// let check = check_ins[i];
if (check_ins[i] === true) {
let alert = this.alertCtrl.create({
title: 'Sorry,',
subTitle: 'ur already checked in',
buttons: ['OK']
});
alert.present();
break;
} else if (check_ins[i] === false || check_ins[i] === undefined) {
let checkInTime = new TimeInModel(in_timing.date, emp_id, in_timing.time_in);
this.employeesService.pushTimeIn(checkInTime)
.subscribe(checkInTime => {
},
error => this.errorMessage = <any>error);
console.log("Successfully checked-in!");
check_ins[i].push(true);
break;
}
可能是什么问题呢?是否有另一种方法来使用数组来实现相同的输出?
你正在做'push'的方式是错误的。它必须是'check_ins.push(true);' –
'check_ins [i] === undefined',所以你不能调用它的函数。另外,当存在一个值时,'check_ins [i]'是一个布尔值。你不能在布尔值上使用'push'功能。你需要使用'check_ins.push(true)' –