[英]How to Filter array from state onChange based on category and revert to default?
For my class based component, I have a state
object that contains a defined array named movies
. 对于基于类的组件,我有一个
state
对象 ,其中包含一个名为movies
已定义数组。 I also have a handleMoviesFilter
function, that is called onChange
on the select
tag. 我还有一个
handleMoviesFilter
函数,在select
标签上调用onChange
。
How can I filter the options dynamically from my array, filter them onChange.. and revert when I pick another movie? 如何从我的数组中动态过滤选项,过滤它们onChange ..并在我选择另一部电影时还原?
state = {
movies: [
{
name: 'Goodfellas',
category: 'crime'
},
{
name: 'Saving private ryan',
category: 'war'
},
{
name: 'The Shawshank Redemption',
category: 'drama'
}
]
}
handleMoviesFilter = e => {
let currentMovie = e.target.value;
const updateMoviesState = this.state.movies.filter(({ category }) =>
currentMovie.includes(category)
);
this.setState({
opleidingen: updateMoviesState,
isResetButtonActive: true,
});
}
<select id="lang" onChange={this.handleMoviesFilter}>
<option value="select">Select</option>
<option value="Goodfellas">Goodfellas</option>
<option value="Saving private ryan">Saving private ryan</option>
<option value="The Shawshank Redemption">The Shawshank Redemption</option>
</select>
If what you are trying to accomplish is based upon the selected option, filter the state.movies and return those in which match, then you would want to do something like the following... 如果你想要完成的是基于所选的选项,过滤state.movies并返回匹配的那些,那么你会想要做类似下面的事情......
state = {
movies: [
{
name: 'Goodfellas',
category: 'crime'
},
{
name: 'Saving private ryan',
category: 'war'
},
{
name: 'The Shawshank Redemption',
category: 'drama'
}
]
}
handleMoviesFilter = e => {
let currentMovie = e.target.value;
const updateMoviesState = this.state.movies.filter(movie => movie.name.toLowerCase() === currentMovie.toLowerCase());
this.setState({
movies: updateMoviesState,
isResetButtonActive: true,
});
}
<select id="lang" onChange={this.handleMoviesFilter}>
<option value="select">Select</option>
<option value="Goodfellas">Goodfellas</option>
<option value="Saving private ryan">Saving private ryan</option>
<option value="The Shawshank Redemption">The Shawshank Redemption</option>
</select>
You would want to 你想要的
toUpperCase()
toUpperCase()
大写所有字符 state
to get the movies that includes the selected value in it's name state
以获取包含其名称中所选值的影片
state = {
movies: [
{
name: 'Goodfellas',
category: 'crime'
},
{
name: 'Saving private ryan',
category: 'war'
},
{
name: 'The Shawshank Redemption',
category: 'drama'
},
{
name: 'Good Riddance',
category: 'drama'
}
]
}
handleMoviesFilter = e => {
let currentMovie = e.target.value.toUpperCase();
//const updateMoviesState = this.state.movies.filter(movie => movie.name.toUpperCase() === currentMovie);
const updateMoviesState = this.state.movies.filter(movie => movie.name.toUpperCase().includes(currentMovie));
this.setState({
movies: updateMoviesState,
isResetButtonActive: true,
});
}
<select id="lang" onChange={this.handleMoviesFilter}>
<option value="select">Select</option>
<option value="Goodfellas">Goodfellas</option>
<option value="Saving private ryan">Saving private ryan</option>
<option value="The Shawshank Redemption">The Shawshank Redemption</option>
</select>
This snippet was made so you can test it out. 制作了这个片段,以便您可以对其进行测试。 This does NOT include react-js framework of course .
当然,这不包括react-js框架 。
const state = { movies: [ { name: 'Goodfellas', category: 'crime' }, { name: 'Saving private ryan', category: 'war' }, { name: 'The Shawshank Redemption', category: 'drama' }, { name: 'Good Riddance', category: 'drama' } ] } handleMoviesFilter = e => { // get requested movie let currentMovie = document.getElementById('textField').value.toUpperCase(); // t console.log("Requested Movie:", currentMovie); // array has a movie that equals selected movie //const updateMoviesState = state.movies.filter(movie => movie.name.toUpperCase() === currentMovie); // array has a movie whose name contain the phrase const updateMoviesState = state.movies.filter(movie => movie.name.toUpperCase().includes(currentMovie)); console.log("Filter Result:", updateMoviesState); }
<input id="textField"></input> <button onclick="handleMoviesFilter(event)">Filter</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.