gpt4 book ai didi

javascript - React/JS 过滤器的问题

转载 作者:行者123 更新时间:2023-11-30 11:09:10 25 4
gpt4 key购买 nike

我正在尝试实现一个过滤器功能,当用户在搜索栏中键入内容时,该功能能够在两个单独的 JSON 字段中进行搜索。搜索整个 JSON 返回错误,如果我重复这个函数,两个相似的函数会相互抵消。

我当前的过滤函数:

let filteredOArt = origArt.filter((origAItem) => {
return origAItem.authors.toLowerCase().includes(this.state.search.toLowerCase())
});

我希望能够在“作者”字段和“描述”字段中进行搜索。

在 React 渲染之前,我有这个监听状态的函数:

updateSearch(event) {
this.setState({ search: event.target.value })
}

然后我的搜索函数在 React 返回的输入字段中:

<h6>Search by author name: <input type="text" value={this.state.search} onChange={this.updateSearch.bind(this)} /></h6>

最佳答案

你可以像这样调整一下函数

let filteredOArt = origArt.filter((origAItem) => {
return (
(origAItem.authors.toLowerCase().includes(this.state.search.toLowerCase())||

(origAItem.description.toLowerCase().includes(this.state.search.toLowerCase())
)
)
});

关于javascript - React/JS 过滤器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54448619/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com