用React过滤对象列表

时间:2018-08-17 18:35:18

标签: javascript reactjs react-redux filtering

这可能是一篇克隆文章,但我没有找到任何解决方案。我有一个对象列表:

export default function() {
  return [
    {name: 'Mark Teer Stegen'},
    {name: 'Nelson Semedo'},
    {name: 'Gerrard Pique'},
    {name: 'Ivan Rakitic'},
    {name: 'Sergio Busquets'},
    {name: 'Denis Suarez'},
    {name: 'Coutinho'},
    {name: 'Luis Suarez'},
    {name: 'Lionel Messi'},
    {name: 'Dembele'},
    {name: 'Malcom'}
  ]
}

我将其导入组件,分配给状态并在下面的组件中显示。

import React, {Component} from 'react';
import {connect} from 'react-redux';

class Barca extends Component{
  constructor(props){
    super(props);

    this.state = {
      players: this.props.players,
      player: '' //empty to set as an input
    }
  }

  onChange(e){
    this.setState({
      player: e.target.value
    });
    console.log(this.state.player);
  }
  renderList(){
    return this.state.players.map((player) => {
      return(
        <tr key={player.name}>
          <td>{player.name}</td>
        </tr>
      );
    });
  }
  render(){
    return(
      <div className="col-sm-6 table-col table-responsive">
        <input
          type="text"
          value={this.state.player}
          onChange={this.onChange.bind(this)}
        />
        <table className="table table-striped">
          <thead>
            <tr>
              <th className="text-center">
                FC Barcelona
              </th>
            </tr>
          </thead>
          <tbody>
            {this.renderList()}
          </tbody>
        </table>
      </div>
    );
  }
}

const mapStateToProps = (state) => {
  return {
    players: state.reducerBarca
   };
};


export default connect(mapStateToProps)(Barca);

列表看起来像这样

list view

问题是我想根据输入值过滤我的球员名单。我在这里做了一些研究,但只在 Array 中找到了过滤条件,与在 Objects list 中的过滤方法不同。

我现在所做的:

  1. 显示玩家列表
  2. 从输入中获取值并在每个书面字母后显示它
  3. 如何通过输入的术语来显示我的列表?

谢谢大家!我已删除玩家状态

constructor(props){
    super(props);

    this.state = {
      //players: this.props.players <-- Stupid thing
      player: '' //empty to set as an input
    }
  }

并重写我的renderList()函数

return this.props.players.filter(player =>
        player.name.toLowerCase().includes(this.state.player.toLowerCase())).map(searchedPlayers => {
          return(
            <tr key={searchedPlayers.name}>
              <td>{searchedPlayers.name}</td>
            </tr>
          );
        })
    }

3 个答案:

答案 0 :(得分:3)

this.state.players.filter(player => player.name.includes(this.state.player))

如果要映射它们而不是仅过滤状态...

this.state.players.filter(player => 
player.name.includes(this.state.player)).map(searchedPlayers => {
  return(
    <tr key={searchedPlayers.name}>
      <td>{searchedPlayers.name}</td>
    </tr>
  );
})

请注意,您也可以直接通过props进行渲染,而无需设置状态(以避免每次用户键入时都重新渲染),方法是替换

this.state.players

使用

this.props.players

答案 1 :(得分:1)

您可以使用过滤器:

let collection = getPlayers();

答案 2 :(得分:0)

您不需要对道具使用复制状态,也可以使用//do something with ...

this.props.players

然后,您可以调用getPlayers() { if(this.state.player) { return this.state.players.filter(player => player.name.includes(this.state.player)) } else { return this.state.players } } 并将播放器映射到html内容。