我正在尝试使用以下代码呈现过滤后的数据:
const notes = [{
title: "Order cat food",
body: "Get 1 kg."
},
{
title: "Clean kitchen",
body: "The fridge door."
},
{
title: "Buy groceries",
body: "Remember the yellow and red pepper."
},
{
title: "Go to the gym",
body: "Exercise for 2hrs."
},
{
text: "Make porridge",
body: "Remember to put lime and honey."
}]
const allfilters = {
searchText: ""
}
const renderNotes = function(notes, filters) {
const filteredNotes = notes.filter(function (note) {
return note.title.toLowerCase().includes(filters.searchText.toLowerCase());
})
console.log(filteredNotes);
}
renderNotes(notes, allfilters);
document.querySelector('#to-do').addEventListener('input', function(e) {
allfilters.searchText = e.target.value;
renderNotes(notes, allfilters);
})
运行后,出现错误“无法读取未定义的属性'lowercase'”。
我想念什么?
答案 0 :(得分:7)
{
text: "Make porridge",
body: "Remember to put lime and honey."
}
您的最后一个数组项没有title
,而是text
,所以在
note.title.toLowerCase()
该数组项note.title
的是undefined
。 toLowerCase()
上不存在undefined
。