Вы хотите, чтобы
- получили выбранное значение
- заглавными буквами все его символы с
toUpperCase()
- фильтр
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>
Test It Out
Этот фрагмент был создан, чтобы вы могли проверить его. Это, конечно, НЕ включает фреймворк Reaction-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>