英文:
Vue.js - how to fill an array while using v-for
问题
我在Vue2的路由项目中工作。我不会解释细节,以尽可能简化这个问题。
我使用v-for
来迭代一个数组。
每个迭代的对象需要添加到一个数组中,我将在模板中发送到另一个组件。
这是一个复杂的JSON。我在Leaflet地图上。
我需要将每个点的坐标与折线连接起来。<l-polyline>
组件期望一个坐标数组(每个坐标都是一个包含2个值的数组)。
所以,我的想法是在迭代第一个列表时将每个object.coordinate
放入一个数组中。然后将该数组作为参数传递给模板中的折线组件。我该如何做到这一点?
<div :key="i" v-for="(route, i) in jsonResult.routesList">
<div :key="j" v-for="(stop, j) in route.stopsList">
<l-marker :lat-lng="latLng(stop.lat, stop.long)" />
// 问题出在这里,因为我只得到一个对象(stop),而我需要获取它们的坐标的整个停靠点列表
<l-polyline :lat-lngs="latLng(stop.lat, stop.long)"></l-polyline>
</div>
</div>
我的JSON大致如下:
<l-polyline>
对象期望绘制线条的数组如下:
[
[20.567934, -103.366844],
[19.54006, -99.1879349],
[25.54193, -100.947906],
[25.7970467, -100.59623]
]
路线列表(代码中的jsonResult.routesList
)
"routesList": [
{
"stopsList": [
{
"id": 1,
"seq": 1,
"start": "2019-09-10T09:32:23",
"end": "2019-09-10T10:17:23",
"lat": 20.567934,
"long": -103.366844
},
{
"id": 2,
"seq": 2,
"start": "2019-09-10T09:32:23",
"end": "2019-09-10T10:17:23",
"lat": 20.587934,
"long": -104.386844
}
]
},
//另一条路线
{
"stopsList": [
{
"id": 1,
"seq": 1,
"start": "2019-09-10T09:32:23",
"end": "2019-09-10T10:17:23",
"lat": 20.567934,
"long": -103.366844
},
{
"id": 2,
"seq": 2,
"start": "2019-09-10T09:32:23",
"end": "2019-09-10T10:17:23",
"lat": 20.587934,
"long": -104.386844
}
]
}
]
英文:
I'm working in a routes project in Vue2. I won't explain the details to make this question as simple as possible.
I'm using a v-for
to iterate through an array.
Each iterated object needs to be added to an array which I'm going to send to another component in my template.
It's a complex JSON. I'm on a leaflet map.
I need to join each point's coordinate with a polyline. The <l-polyline>
component is expecting an array of coordinates (each coordinate is an array of 2 values).
So, my take is to put each object.coordinate
into an array as I iterate over the first list. Then that array is going to be passed as a parameter to the polyline component in the template. How do I do that?
<div :key="i" v-for="(route, i) in jsonResult.routesList">
<div :key="j" v-for="(stop, j) in route.stopsList">
<l-marker :lat-lng="latLng(stop.lat, stop.long)" />
// the problem is here because I'm only getting one object (stop) and I need the whole list of stops to get their coordinates
<l-polyline :lat-lngs="latLng(stop.lat, stop.long)"></l-polyline>
</div>
</div>
My JSON looks more or less like this:
The <l-polyline>
object expects an array like this to draw the lines:
[
[20.567934, -103.366844],
[19.54006, -99.1879349],
[25.54193, -100.947906],
[25.7970467, -100.59623]
]
The routes list (jsonResult.routesList
in the code)
"routesList": [
{
"stopsList": [
{
"id": 1,
"seq": 1,
"start": "2019-09-10T09:32:23",
"end": "2019-09-10T10:17:23",
"lat": 20.567934,
"long": -103.366844,
},
{
"id": 2,
"seq": 2,
"start": "2019-09-10T09:32:23",
"end": "2019-09-10T10:17:23",
"lat": 20.587934,
"long": -104.386844,
}
],
},
//another route
{
"stopsList": [
{
"id": 1,
"seq": 1,
"start": "2019-09-10T09:32:23",
"end": "2019-09-10T10:17:23",
"lat": 20.567934,
"long": -103.366844,
},
{
"id": 2,
"seq": 2,
"start": "2019-09-10T09:32:23",
"end": "2019-09-10T10:17:23",
"lat": 20.587934,
"long": -104.386844,
}
],
},
答案1
得分: 3
我认为问题出在你使用<div>
的方式上,当你说:
<div :key="j" v-for="(stop, j) in route.stopsList">
<l-marker :lat-lng="latLng(stop.lat, stop.long)" />
// 这里的问题在于我只得到了一个对象(stop),而我需要整个停靠点列表来获取它们的坐标
<l-polyline :lat-lngs="latLng(stop.lat, stop.long)"></l-polyline>
</div>
在v-for
中,你正在将每个单独的元素从stopList
传递给<l-polyline>
组件。我认为正确的方法是将整个stopList
传递给组件,然后进行相应的格式化(在<l-polylist>
组件内部迭代stopList
数组)。
<div :key="i" v-for="(route, i) in jsonResult.routesList">
<div :key="j" v-for="(stop, j) in route.stopsList">
<l-marker :lat-lng="latLng(stop.lat, stop.long)" />
<l-polyline :lat-lngs="route.stopsList.map(x=>[x.lat,x.long])"></l-polyline>
</div>
</div>
当然,这不是最优化的方法,但它将解决你的问题。更好的方式是使用基于Dawid Zbiński的思路,并使用计算属性。希望这对你有所帮助。
英文:
I think the problem is in the way that your using your <div>
, when you say:
<div :key="j" v-for="(stop, j) in route.stopsList">
<l-marker :lat-lng="latLng(stop.lat, stop.long)" />
// the problem is here because I'm only getting one object (stop) and I need the whole list of stops to get their coordinates
<l-polyline :lat-lngs="latLng(stop.lat, stop.long)"></l-polyline>
</div>
In the v-for
you were passing each individual element from the stopList
to the <l-polyline>
component. I think the correct approach would be to pass the whole stopList
to the component, and then format it accordingly (iterating over the stopList
array is done inside the <l-polylist>
component.
<div :key="i" v-for="(route, i) in jsonResult.routesList">
<div :key="j" v-for="(stop, j) in route.stopsList">
<l-marker :lat-lng="latLng(stop.lat, stop.long)" />
<l-polyline :lat-lngs="route.stopsList.map(x=>[x.lat,x.long])"></l-polyline>
</div>
</div>
Of course this is not an optimal approach, but it will solve your problem, a better way would be using something based on the idea of Dawid Zbiński and use a computed property. I hope this helps
答案2
得分: 2
你应该使用一个方法来计算要传递给另一个组件的坐标数组。
// Vue 组件文件
methods: {
/*
* 将 stopsList 数组转换为坐标数组。
* 返回值示例:
* [
* [ 23.1234, 21.2322 ],
* [ 21.1242, 24.2333 ],
* ]
*/
getStopsCoordinates(stops) {
return stops.map(stop => [stop.lat, stop.long]);
}
}
然后,当您传递所有站点的停车坐标时,可以使用 getStopsCoordinates
方法,如下所示:
<div :key="i" v-for="(route, i) in jsonResult.routesList">
<div :key="j" v-for="(stop, j) in route.stopsList">
<l-marker :lat-lng="latLng(stop.lat, stop.long)" />
// 问题在这里,因为我只获取到一个对象(stop),我需要整个站点列表以获取它们的坐标
<l-polyline :lat-lngs="getStopsCoordinates(route.stopsList)"></l-polyline>
</div>
</div>
虽然这应该可以工作,但不是最佳解决方案,因为列表在第二个 v-for
中每次都被转换,这可能是不必要的。有多种方法可以进行优化,但没有测量等信息,我可能会选择为 routesList
创建一个 getter。
// Vue 组件文件
getters: {
/* 返回具有属性 stopsCoordinates 的 routesList */
routesListWithStopsCoordinates() {
return this.jsonResult.routesList.map(route => {
route.stopsCoordinates = this.getStopsCoordinates(route.stopsList);
return route;
})
}
},
methods: {
/* 如上所述 */
getStopsCoordinates(stops) {
return stops.map(stop => [stop.lat, stop.long]);
}
}
然后在您的模板中可以这样做:
<div :key="i" v-for="(route, i) in jsonResult.routesList">
<div :key="j" v-for="(stop, j) in route.stopsList">
<l-marker :lat-lng="latLng(stop.lat, stop.long)" />
// 问题在这里,因为我只获取到一个对象(stop),我需要整个站点列表以获取它们的坐标
<l-polyline :lat-lngs="route.stopsCoordinates"></l-polyline>
</div>
</div>
希望这对您有帮助。
英文:
You should use a method in order to compute the array of coordinates you want to pass to the other component.
// Vue component file
methods: {
/*
* Converts the stopsList array to an array of coordinates.
* Example of returned value:
* [
* [ 23.1234, 21.2322 ],
* [ 21.1242, 24.2333 ],
* ]
*/
getStopsCoordinates(stops) {
return stops.map(stop => [ stop.lat, stop.long ]);
}
}
You can then use the getStopsCoordinates
method when you're passing the stops coordinates of all stops, like so:
<div :key="i" v-for="(route, i) in jsonResult.routesList">
<div :key="j" v-for="(stop, j) in route.stopsList">
<l-marker :lat-lng="latLng(stop.lat, stop.long)" />
// the problem is here because I'm only getting one object (stop) and I need the whole list of stops to get their coordinates
<l-polyline :lat-lngs="getStopsCoordinates(route.stopsList)"></l-polyline>
</div>
</div>
Although it should work, it's not an optimal solution, as the list is converted each time in the second v-for
and it's probably not needed. There's multiple ways on how this can be optimised, but without the measurements etc. I'd probably go with a getter for routesList
.
// Vue component file
getters: {
/* Returns routesList where each route has property stopsCoordinates */
routesListWithStopsCoordinates() {
return this.jsonResult.routesList.map(route => {
route.stopsCoordinates = this.getStopsCoordinates(route.stopsList);
return route;
})
}
},
methods: {
/* Already explained above */
getStopsCoordinates(stops) {
return stops.map(stop => [ stop.lat, stop.long ]);
}
}
Then in your template you can do something like this:
<div :key="i" v-for="(route, i) in jsonResult.routesList">
<div :key="j" v-for="(stop, j) in route.stopsList">
<l-marker :lat-lng="latLng(stop.lat, stop.long)" />
// the problem is here because I'm only getting one object (stop) and I need the whole list of stops to get their coordinates
<l-polyline :lat-lngs="route.stopsCoordinates"></l-polyline>
</div>
</div>
Hope it works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论