我想在数组中的特定索引处推送布尔值。每当我运行代码时,我都会收到此错误,错误类型错误:无法读取未定义的属性"push">
这是我的代码:
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;
}
可能是什么问题?使用数组实现相同输出的另一种选择吗?
你不需要将值true
推入check_ins[i]
,你可以设置它: check_ins[i] = true
或推 true 到数组: check_ins.push(true)
使用 push 时,推送的值将作为新元素添加到数组中。我建议您执行以下操作:
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, true);
this.employeesService.pushTimeIn(checkInTime)
.subscribe(checkInTime => {
},
error => this.errorMessage = <any>error);
console.log("Successfully checked-in!");
check_ins[i]=true;
break;
}