javascript - 我如何在 React 中对我的数据进行多个过滤器?

标签 javascript reactjs filter javascript-objects

我在 React 数据列表中使用多个过滤器时遇到问题。目前我有一个过滤器在工作,如果数据的城市与过滤器城市匹配,它只返回列表项,但我如何同时过滤价格、城市,并一起搜索?

这是我的组件

            import React, { Component } from 'react'
            import ListingStreamItem from './ListingStreamItem'
            import zipcodes from 'zipcodes'
            class Home extends Component {
                constructor(props) {
                    super(props)
                    this.handleChange = this.handleChange.bind(this)
                    this.state = {
                        visitor_city: '',

                    }
                }
                componentDidMount() { 
                    const _this = this;
                    $.get("https://ipinfo.io", function(response) {
                        console.log(response.city, response.country);
                        _this.setState({
                            visitor_city: response.city
                        })
                    }, "jsonp");

                }
                handleChange(e) {
                    this.setState({
                        [e.target.name] : e.target.value
                    })
                }
                render(){

                    const user = this.props.user !== null ? true : false
                    const listings = this.props.listings == null ? 'There are no listings available at this time' : Object.keys(this.props.listings).reverse().map((key) => {
                        var areEqual = this.state.visitor_city.toUpperCase() === this.props.listings[key].city.toUpperCase();

                        if (areEqual) {
                            return (
                                <li key={key}>
                                    <ListingStreamItem 
                                        id={key}
                                        title={this.props.listings[key].title}
                                        desc={this.props.listings[key].description}
                                        price={this.props.listings[key].price}
                                        zipcode={this.props.listings[key].zipcode}
                                        images={this.props.listings[key].listingimages}
                                    />
                                </li>
                            )
                        }
                    }) 
                    return(
                        <div>

                        <select name="visitor_city" onChange={this.handleChange}>
                            <option value="orlando">Orlando </option>
                            <option value="new york city">New York City</option>
                        </select>
                        <p>Or Zip Code </p>
                        <input type='number' min="0" name='miles_from_zip' placeholder="miles" value={this.state.miles_from_zip} />
                        <input type='text' name="zipcode" placeholder="from zip" value={this.state.zipcode} />
                        <button>Filter </button>
                        <ul>
                            {listings}
                        </ul>

                        </div>
                    )
                }
            }

            export default Home

最佳答案

为什么你不能在你的 map 中做另一个 if (这是假设你的价格过滤器也处于状态的简单解决方案):

const listings = this.props.listings == null ? 'There are no listings available at this time' : Object.keys(this.props.listings).reverse().map(key => {
    let {visitor_city, price} = this.state;
    let listing = this.props.listings[key];

    if (visitor_city.toUpperCase() === listing.city.toUpperCase()
        && (!price || price === listing.price)
    ) {
        return (
        ...
        )
    }
}) 

更清晰的选择可能是查看 Array.prototype.filter功能:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

var words = ["spray", "limit", "elite", "exuberant", "destruction", "present"];

var longWords = words.filter(word => word.length > 6);

// Filtered array longWords is ["exuberant", "destruction", "present"]

所以在你的场景中,也许你可以做类似的事情:

Object.keys(this.props.listings).reverse().filter(key => {
    let {visitor_city, price} = this.state;
    let listing = this.props.listings[key];

    return visitor_city.toUpperCase() === listing.city.toUpperCase()
        && (!price || price === listing.price)    
}).map(key => {
    ...
}) 

关于javascript - 我如何在 React 中对我的数据进行多个过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46100476/

相关文章:

r - 选择包含特定值的行的组(使用 dplyr 和管道)

javascript - Chrome 中的地理定位 API : coord property of Position undefined

javascript - 在 ie 7 和 6 上更改 href 时出现 Fancybox 问题

reactjs - 如何运行 NPM RUN BUILD

javascript - 当 mapStateToProps 中收到另一个 prop 时更新 prop

Magento 发票网格 filter_condition_callback 不起作用

PHP删除数组的第一个索引并重新索引

javascript - 使用 ajax 无法在灯箱中打开图像

javascript - 替换 JavaScript 中 URL 字符串中的特定值

javascript - 使用 Redux 容器组件模式渲染动态数量的 React 子组件的稳健方法是什么?