【问题标题】:Value from for each loop doesn't get returned不会返回每个循环的值
【发布时间】:2020-02-08 23:50:33
【问题描述】:

我的 Vue 组件中有一个条件 for-loop。我只想循环通过以“纽约”为目的地的火车站,或者“纽约”在路线上。

<tr v-for="departure in filterTrainStations" :key="departure.name">
            <td>{{ departure.product.categoryCode }}</td>
            <td>{{ departure.direction }}</td>
            <td>{{ getTimeFromDate(departure.actualDateTime) }}</td>
            <td>{{ departure.name }} </td>
        </tr>

这是我和for-loop 的桌子。它目前向我展示了所有以“纽约”为方向的火车。但也有其他方向的火车,但路线上有“纽约”。所以我也想检查那些。这是我计算出来的filerTrainStations 函数:

filterTrainStations: function() {

    // Direction where the train has to go to.
    const city = this.city.mediumName; // New York

    return this.response.payload.departures.filter(function(u) {
        // Only return the trains which go to the right direction. 
        if(u.direction === city) {
            return u;
        }   

        // Check if there are any trains with the desired city on their route.
        u.routeStations.forEach(function (arrayItem) {
            if(arrayItem.mediumName === city) {
                console.log(u);
                return u;
            }                    
        })
    }
)},

应该从forEach 函数返回的值不会返回任何内容,但是当我console.log 它向我显示正确的信息时。

我的问题是:

为什么没有返回值u 的值?

我从 API 中检索出发信息的响应如下所示:

response: {
          "links": {},
            "payload": {
                "source": "PPV",
                "departures": [
                    {
                        "direction": "Weert",
                        "name": "NS  5249",
                        "plannedDateTime": "2019-10-10T15:08:00+0200",
                        "plannedTimeZoneOffset": 120,
                        "actualDateTime": "2019-10-10T15:08:00+0200",
                        "actualTimeZoneOffset": 120,
                        "plannedTrack": "1",
                        "product": {
                            "number": "5249",
                            "categoryCode": "SPR",
                            "shortCategoryName": "NS Sprinter",
                            "longCategoryName": "Sprinter",
                            "operatorCode": "NS",
                            "operatorName": "NS",
                            "type": "TRAIN"
                        },
                        "trainCategory": "SPR",
                        "cancelled": false,
                        "routeStations": [
                            {
                                "uicCode": "8400129",
                                "mediumName": "Boxtel"
                            },
                            {
                                "uicCode": "8400206",
                                "mediumName": "Eindhoven"
                            },
                            {
                                "uicCode": "8400245",
                                "mediumName": "Geldrop"
                            }
                        ],
                        "departureStatus": "INCOMING"
                    },
                ]
            },
            "meta": {}
          }

【问题讨论】:

  • 这是因为它将返回内部闭包(forEach)而不是外部闭包(过滤器)。看起来使用.some 会是更好的选择。
  • 目前尚不清楚forEach 的用途。它在filter 里面,这是一个奇怪的地方。 filter 是否意味着检查两个条件然后返回 usome 在这种情况下可能更合适。

标签: javascript arrays for-loop vue.js


【解决方案1】:

为什么没有返回值u的值?

因为return 语句只是从传递给forEach 的函数返回,它不会从外部filter 方法返回。您应该改用 find 并返回结果:

filterTrainStations: function() {

    // Direction where the train has to go to.
    const city = this.city.mediumName; // New York

    return this.response.payload.departures.filter(function(u) {
        // Only return the trains which go to the right direction. 
        if(u.direction === city) {
            return u;
        }   

        // Check if there are any trains with the desired city on their route.
        return u.routeStations.find(arrayItem => arrayItem.mediumName === city);
    }
)},

但这引发了另一个问题 - 代码行本身位于 filter 中,您应该在其中返回 truefalse,具体取决于您是否希望该项目包含在结果中。如果没有更多信息,很难调和您遇到的实际问题。但是,返回 anything truthy 将导致 departures 项目包含在结果中。

就个人而言,我会按照MKougiouris answer 的建议去做,因为它更清晰。

【讨论】:

  • filter 内部返回值确实感觉不对。我只想在方向匹配城市或城市在 routeStations 数组内时返回所有内容。
  • @Baspa 看到另一个答案。这是实现目标的更好方法。
【解决方案2】:

您的过滤器回调不适合您想要实现的目标。 试试下面这个:

filterTrainStations: function() {

    // Direction where the train has to go to.
    const city = this.city.mediumName; // New York

    return this.response.payload.departures.filter(function(u) {
        return u.direction == city || u.routeStations.some(function(rs){ rs.mediumName == city});
    });
)}

如果这有帮助,请告诉我

编辑:进行了编辑以包含您的代码,以便您可以更轻松地了解如何使用它!

【讨论】:

  • 你可能是对的,但是通过返回任何真实的过滤器将起作用,只要 some 返回 true (在你的情况下)或 find 返回非空(在我的)过滤器可能会按照他们的预期工作。
  • 是的,你的答案也是一个可靠的答案,我唯一要改变的是格式,这样你最终只会得到一个返回,而不是 OP 的原始代码格式
  • 谢谢,我在设计上保持代码接近 OP,但我自己不会那样做。我已经向他们指出了您的答案,哪个 IMO 是正确的答案。
  • 函数现在只返回truefalse,但没有数据。我相信您的回答是正确的,因为它比我自制的function 要清楚得多,但是如何返回数据而不是仅返回truefalse?还是我必须在table 中修改我的v-for? @MKougiouris
  • 我进行了编辑以更好地理解语句的位置,如果您按原样使用它应该可以工作!
猜你喜欢
  • 2015-04-19
  • 2022-11-23
  • 1970-01-01
  • 1970-01-01
  • 2016-05-21
  • 1970-01-01
  • 2016-03-24
  • 2021-11-30
  • 2013-12-30
相关资源
最近更新 更多