使用indexOf的javascript搜索不';不使用map()工作
尝试使用react隐藏和显示列表,但失败使用indexOf的javascript搜索不';不使用map()工作,javascript,reactjs,ecmascript-6,Javascript,Reactjs,Ecmascript 6,尝试使用react隐藏和显示列表,但失败 handleChange = e => { const { list } = this.state; if (e.target.value !== "") { this.setState({ list: list.map(o => ({ name: o.name, hide: !o.name.indexOf(e.target.value) > -1
handleChange = e => {
const { list } = this.state;
if (e.target.value !== "") {
this.setState({
list: list.map(o => ({
name: o.name,
hide: !o.name.indexOf(e.target.value) > -1
}))
})
} else {
this.setState({
list: list.map(o => ({
name: o.name,
hide: false
}))
});
}
};
有什么线索吗 让我解释一下你做错了什么
隐藏:!o、 name.indexOf(e.target.value)>-1
在上面的代码中,indexOf值将被返回(可能是某个数字),然后返回false,因为代码>操作员
您试图将BOOL值与整数-1进行比较,该整数将明显返回false
用括号括起您的病情,然后放入代码>如下所示
隐藏:!(o.name.indexOf(e.target.value)>-1)
或使用以下条件
hide:o.name.indexOf(e.target.value)==-1
它会起作用的