简体   繁体   English

如何根据类别从状态onChange过滤数组并恢复为默认值?

[英]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.movi​​es并返回匹配的那些,那么你会想要做类似下面的事情......

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 你想要的

  • get the selected value 获取所选值
  • uppercase all of it's characters with toUpperCase() 使用toUpperCase() 大写所有字符
  • filter state to get the movies that includes the selected value in it's name 过滤 state以获取包含名称中所选值的影片
    • filter & includes method used 过滤器并包括使用的方法
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 测试一下

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.

相关问题 从更新状态<select>使用 onChange 标记并过滤类别列表以显示来自该类别的博客文章 - Update state from <select> tag using onChange and filter through list of categories to display blog posts from that category 如何在react中根据类别过滤数组? - How can I filter an array based on a category in react? 如何从每个类别的 Json 数组中过滤和发送数据? - How to Filter and Send Data from a Json Array per Category? 如何使用 reactJs 中的 onChange 过滤我的 State? - How can i filter my State with onChange in reactJs? 如何根据类别动态过滤JSON数据 - How to dynamically filter JSON data based on category 基于键如何在JavaScript中从数组中过滤? - Based on key how to filter from array in JavaScript? 如何处理数组内对象的 onChange(反应状态)? - How do I handle onChange of objects inside an array (React State)? 如何使用 onChange 和 setState() 将事件目标推送到 state 中数组中的 object - How to push event targets to an object in an array in state with onChange and setState() 如何使用基于另一个数组的值过滤对象数组 - How to filter array of objects with based on value from another array 如何根据另一个数组过滤来自一个数组的数据 - How to filter data from one array based on another array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM