foreach如何在react-redux中工作

时间:2018-01-21 20:13:53

标签: node.js reactjs foreach react-redux

我正在创建一个可以从KIWI api获取航班的nodejs应用程序,它会返回一个json列表,并且由于您有来自和来的参数,它应该返回一个航班列表。

我能成功地做到这一点,但是当我想展示一切我不知道该怎么做的时候。

这是我的渲染:

render(){
        return (
            <table className="table table-hover">
                <thead>
                    <tr>
                        <th>Flight #</th>
                        <th>From</th>
                        <th>To</th>
                    </tr>
                </thead>
                <tbody>
                    {this.props.flights.map(this.renderFlights)}
                </tbody>
            </table>
        );
    }

renderFlights(flightData){
        // add key to make them unique
        return(
            <tr>
                <td>{flightData.data[0].id}</td>
                <td>{flightData.data[0].mapIdfrom}</td>
                <td>{flightData.data[0].mapIdto}</td>
            </tr>
        );
    }

{this.props.flights.map(this.renderFlights)}只是映射数组中的第一个,我知道我必须使用foreach,但我不知道如何使用它来打印列表中的所有内容,航班ID加上来自和去,所以当你拿到航班你到15点左右,我希望能够显示所有15个航班,有人可以帮助我吗?

这会为forEach返回undefined:

 <tbody>
                        {
                            this.props.flights.array.forEach(element => {
                                this.renderFlights
                            })
                        }
                    </tbody>

2 个答案:

答案 0 :(得分:4)

我在此处找到了您的API和测试界面:https://skypickerbookingapi1.docs.apiary.io/#reference/check-flights/checkflights/check_flights?console=1

所以你似乎得到了这个对象作为你的回应:

{
  "server_time": 1516568910,
  "flights_checked": false,
  "extra_fee": 0,
  // blah blah,

  "flights": [

    // first flight 

    {
      "bags_recheck_required": false,
      "dtime_unix": 1524646800,
      "extra": "",
      "atime_unix": 1524651600,
      "priority_boarding": {
        "currency": null,
        "price": null,
        "is_possible": false
      },
      "price": 313,
      "currency": "NOK",
      "price_new": 313,
      // blah blah
    },

    // second flight
   {
      "bags_recheck_required": true,
      "dtime_unix": 1524683400,
      "extra": "",
      "atime_unix": 1524691800,
      "priority_boarding": {
        "currency": null,
        "price": null,
        "is_possible": false
      },
      "price": 1560,
      "currency": "NOK",
      "price_new": 1560,
      // blah blah
   },
   // more and more flights

所以我猜你的“this.props.flights”指的是上述对象的“flight”属性。

现在,你需要使用“map”,而不是“foreach”:

this.props.flights.map(this.renderFlights) //this is correct

你的回调函数应该是:

renderFlights(one_single_flight){
        // add key to make them unique
        return(
            <tr>
                <td>{one_single_flight.id}</td>
                <td>{one_single_flight.src_country}</td>
                <td>{one_single_flight.dst_country}</td>
            </tr>
        );
    }
}

答案 1 :(得分:0)

这就是我如何获得我想要的结果:

renderFlights(flightData){
        const result = flightData._results;       
        var rows = [];
        for (var i = 0; i < result; i++) {
            rows.push(
            <tr key={i} >
                <td>{flightData.data[i].mapIdfrom}</td>,
                <td>{flightData.data[i].mapIdto}</td>,
            </tr>
            );
        }
        return <tbody>{rows}</tbody>;
    }

在某些日期打开所有可用的航班。