【发布时间】: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": {}
}
【问题讨论】:
标签: javascript arrays for-loop vue.js