多维数组关联
2020-11-05
59
我有两个数组,routesFeatures 和 stopsFeatures,它们分别包含车辆路线的属性和每个站点的属性。它们通过其名称属性相互关联,这意味着如果 stopsFeatures 中任何条目的 RouteName 属性与 routesFeatures 中任何条目的 Name 属性匹配,则意味着停靠点属于该路线。 现在,从代码角度来看,我正尝试进行这种匹配,并将它们放在一个二维数组中,其中每个索引都是一条路线,并且该索引内有相应的站点。
这是 routesFeatures:
[{
ObjectId: 1,
Name: "Rota 1"
},
{
ObjectId: 2,
Name: "Rota 2"
},
...
]
这是 stopFeatures:
[{
ObjectId: 1,
Name: "Carga 0",
RouteName: "Rota 2"
},
{
ObjectId: 2,
Name: "Descarga 0",
RouteName: "Rota 2"
},
...,
{
ObjectId: 11,
Name: "Carga 5",
RouteName: "Rota 4"
},
...
]
这是我的功能:
function drawRoutesOptimized(routesFeatures, stopsFeatures){
var stopsRoutes = [[]];
for(var i=0; i<routesFeatures.length; i++){
// draw some stuff on screen
var count = 0;
for(var j=0; j<stopsFeatures.length; j++){
if(stopsFeatures[j].attributes.RouteName == routesFeatures[i].attributes.Name){
stopsRoutes[i][count] = stopsFeatures[j].attributes.Name;
count++;
}
}
}
}
这是 endsRoutes 的 console.log,它按预期工作,但只有当 i 为 0 时才会崩溃:
[0]
[0] "Carga 2"
[1] "Descarga 2"
[2] "Carga 6"
[3] "Descarga 6"
[4] "Carga 7"
[5] "Descarga 7"
在我看来,这应该可以工作,但我得到了: 未捕获(在承诺中)TypeError:无法设置未定义的属性“0” 当 i 变为值 1 时
2个回答
从逻辑上讲,这非常简单。
- 循环遍历路线
- 每个路线,循环遍历站点
-
添加
RouteName
与路线匹配的站点
这不是最优化的解决方案,但它有效。请记住,我使用 reduce 将每条路线初始化为数组。
const routesFeatures = [{
ObjectId: 1,
Name: "Rota 1"
},
{
ObjectId: 2,
Name: "Rota 2"
},
];
const stopFeatures = [{
ObjectId: 1,
Name: "Carga 0",
RouteName: "Rota 2"
},
{
ObjectId: 2,
Name: "Descarga 0",
RouteName: "Rota 2"
},
{
ObjectId: 11,
Name: "Carga 5",
RouteName: "Rota 4"
},
];
const result = routesFeatures.reduce((acc, routeFeature) => {
acc.push(stopFeatures.reduce((_acc, stopFeature) => {
if (stopFeature.RouteName === routeFeature.Name) {
_acc.push(stopFeature.RouteName)
}
return _acc;
}, []))
return acc
}, []);
console.log(result);
使用标准 for 循环
const routesFeatures = [{
ObjectId: 1,
Name: "Rota 1"
},
{
ObjectId: 2,
Name: "Rota 2"
},
];
const stopsFeatures = [{
ObjectId: 1,
Name: "Carga 0",
RouteName: "Rota 2"
},
{
ObjectId: 2,
Name: "Descarga 0",
RouteName: "Rota 2"
},
{
ObjectId: 11,
Name: "Carga 5",
RouteName: "Rota 4"
},
];
function drawRoutesOptimized(routesFeatures, stopsFeatures) {
const routeStops = [];
for (let i = 0; i < routesFeatures.length; i++) {
routeStops.push([]);
const routeArray = routeStops[i];
const route = routesFeatures[i];
for (let j = 0; j < stopsFeatures.length; j++) {
const stop = stopsFeatures[j];
if (stop.RouteName === route.Name) {
routeArray.push(stop.RouteName)
}
}
}
return routeStops
}
const result = drawRoutesOptimized(routesFeatures, stopsFeatures);
console.log(result);
Dane Brouwer
2020-11-05
不太确定您实际上想要什么,我删除了属性参数以使其在代码片段中起作用。您忘记声明子数组,因此当路由无法将数据推送到数组中时。
let routesFeatures = [{
ObjectId: 1,
Name: "Rota 1"
},
{
ObjectId: 2,
Name: "Rota 2"
}
]
let stopsFeatures = [{
ObjectId: 1,
Name: "Carga 0",
RouteName: "Rota 2"
},
{
ObjectId: 2,
Name: "Descarga 0",
RouteName: "Rota 2"
}
]
function drawRoutesOptimized(routesFeatures, stopsFeatures){
var stopsRoutes = [];
for(var i=0; i<routesFeatures.length; i++){
// draw some stuff on screen
var count = 0;
stopsRoutes[i] = [];
for(var j=0; j<stopsFeatures.length; j++){
if(stopsFeatures[j].RouteName == routesFeatures[i].Name){
stopsRoutes[i][count] = stopsFeatures[j].Name;
count++;
}
}
}
console.log(stopsRoutes)
}
drawRoutesOptimized(routesFeatures, stopsFeatures)
syarul
2020-11-05