我有对象的mapsOrder数组和mapsData数组:
let mapsOrder = [1,2,1,3];
let mapData = [
{
id: 1,
gates: [
{
toId: 2,
coords: {
x: 2,
y: 42
}
},
{
toId: 3,
coords: {
x: 9,
y: 4
}
}
]
},
{
id: 2,
gates: [
{
toId: 1,
coords: {
x: 6,
y: 5
}
}
]
},
{
id: 3,
gates: [
{
toId: 1,
coords: {
x: 2,
y: 1
}
}
]
}
]
我要实现的是基于数组中id的mapsOrder位置的循环,指定到下一个映射的门。mapsOrdermapData
因此,我们有一个循环迭代4次,并在以下时间循环:
循环索引是1当前映射是1下一个映射是2,到下一个的门是 coords: { x: 2, y: 42 }
循环索引是2当前映射是2下一个映射是1,到下一个的门是 coords: { x: 6, y: 5 }
循环索引是3当前映射是1下一个映射是3,到下一个的门是 coords: { x: 9, y: 4 }
循环索引是4当前地图是3下一个地图是1,到下一个的门是 coords: { x: 2, y: 1 }
最后一次循环迭代请参见下一个映射为mapsOrder数组的第一个。我尝试自己先确定下一张地图的ID,如下所示:
for(let i = 0; i < mapsOrder.length; i++) {
let nextMap;
let currentMapId = mapData[mapsOrder[i] - 1].id;
if(i === mapsOrder.length - 1) {
nextMap = mapData[0].id
} else {
nextMapId = mapData[mapsOrder[i]].id;
}
console.log('Current map is: ', currentMapId, 'and the next map id is:', nextMapId)
console.log('break-----')
}
婷婷同学_
慕的地10843
相关分类