【问题标题】:Value in object won't change对象中的值不会改变
【发布时间】:2021-05-08 02:43:42
【问题描述】:
const filterField = {
  searchText: ''
  //Empty value to change over time.
}

const renderNotes = function (notes, filters) {
  const filteredNotes = notes.filter(function (note) {
    return note.title.toLowerCase().includes(filters.searchText.toLowerCase())
  }) 
  console.log(filteredNotes)
}
renderNotes(habits, filterField)

//This function is supposed to change the value of the filteredField object. 
  document.querySelector('#search-text').addEventListener('input', function (e) {
    filterField.searchText === e.target.value;
    renderNotes(habits, filterField);
  } ) 

最后一个方法应该是改变 filterfield 对象的值。但它不会改变。

我正在关注关于 udemy 的教程,但我被困在这部分。

【问题讨论】:

  • === 是比较,赋值是=
  • 我耐心地等待着自己像个白痴。谢谢!

标签: javascript function javascript-objects


【解决方案1】:
filterField.searchText === e.target.value;

等号太多:

  • 双重相等检查(值)
  • 严格相等的三重相等检查(值 AND 类型)
  • 单等分配

改成

filterField.searchText = e.target.value;

【讨论】:

  • 请不要对此类微不足道的错误发布真正的答案。它会延迟自动删除。
猜你喜欢
  • 1970-01-01
  • 2021-09-12
  • 1970-01-01
  • 2022-12-11
  • 1970-01-01
  • 2018-07-20
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
相关资源
最近更新 更多