当我尝试将对象推送到数组时出现以下错误。
未捕获的TypeError:无法在HTMLButtonElement.setTimes中读取未定义的属性'push'
...但是afaik,push(times)的属性被定义了。那么问题是什么? 这是我的代码
let timeDB = {
mon: [],
tue: [],
wed: [],
thu: [],
fri: [],
sat: [],
sun: []
};
// Save times to database
let setTimes = function(){
let inputs = document.getElementsByClassName('timeContainer');
//console.log(inputs);
for(let i = 0; i < inputs.length; i++) {
let day = inputs[i].parentNode.id;
//console.log(day);
let times = {
start: inputs[i].childNodes[0].value,
end: inputs[i].childNodes[1].value
};
console.log(times);
timeDB.day.push(times);
//console.log(inputs[i].parentNode.id + " " + "start time: " + inputs[i].childNodes[0].value);
//console.log(inputs[i].parentNode.id + " " + "end time: " + inputs[i].childNodes[1].value);
//console.log(inputs[i].parentNode.id);
}
};
let saveButton = document.getElementById("saveTimes").addEventListener( 'click', setTimes );
以下是完整的fiddle
由于
答案 0 :(得分:1)
使用:timeDB[day].push(times);