通过mapsOrder数组确定下一张地图

我有对象的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-----')


}


精慕HU
浏览 154回答 3
3回答

婷婷同学_

如果您不在乎原始数组,则只需使用shift下一个门即可(shift将从阵列中删除该门,因此当再次遇到该对象时,下一个门将可用)。用于find从数组中查找对象:let result = mapsOrder.map(id =>&nbsp; &nbsp; mapData.find(o => o.id == id).gates.shift().coords);您可能想find在使用之前检查是否确实找到了某些东西,并且gates数组包含某些东西shift,这是一种更安全的方法:let result = mapsOrder.map(id => {&nbsp; &nbsp; let obj = mapData.find(o => o.id == id);&nbsp; &nbsp; if(obj && obj.gates.length) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // if we found an object with the same id and that object still have gates&nbsp; &nbsp; &nbsp; &nbsp; return obj.gates.shift().coords;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// return the coords of the first gate and remove the gate from the array&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // otherwise, throw an error or something});不可更改:而不是使用shift前面的示例,我们将使用一个对象来跟踪gates数组中的门索引:let nextGateIndex = Object.create(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// create a prototypeless object to track the next gate index for each objectlet result = mapsOrder.map(id => {&nbsp; &nbsp; let obj = mapData.find(o => o.id == id);&nbsp; &nbsp; let index;&nbsp; &nbsp; if(nextGateIndex[id] == undefined) {&nbsp; &nbsp; &nbsp; &nbsp; index = 0;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; index = nextGateIndex[id] + 1;&nbsp; &nbsp; }&nbsp; &nbsp; nextGateIndex[id] = index;&nbsp; &nbsp; if(obj && index < obj.gates.length) {&nbsp; &nbsp; &nbsp; &nbsp; return obj.gates[index].coords;&nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // throw error or something});

慕的地10843

如果按照您的描述,您的循环应类似于。似乎您要使用id和toId使用数组索引。用对象替换数组可能是一个好主意。for(let i = 0; i < mapsOrder.length; i++) {&nbsp; let nextMap;&nbsp; let currentMapId = mapsOrder[i];&nbsp; if(i === mapsOrder.length - 1) {&nbsp; &nbsp; nextMapId = mapsOrder[0]&nbsp; &nbsp;&nbsp; } else {&nbsp; &nbsp; nextMapId = mapsOrder[i + 1];&nbsp; &nbsp;&nbsp;&nbsp; }&nbsp; let filteredMapData = mapData.filter(f => f.id == currentMapId);&nbsp; let filteredGates = filteredMapData.length > 0 ? filteredMapData[0].gates.filter(f => f.toId == nextMapId) : [];&nbsp; console.log('Current map is: ', currentMapId, 'and the next map id is:', nextMapId, 'gates:', filteredGates.length == 0 ? "no gates": filteredGates[0].coords)&nbsp; console.log('break----')}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript