为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

精慕HU 2021-04-30 12:13:27
我有对象的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-----')}
查看完整描述

3 回答

?
婷婷同学_

TA贡献1844条经验 获得超8个赞

如果您不在乎原始数组,则只需使用shift下一个门即可(shift将从阵列中删除该门,因此当再次遇到该对象时,下一个门将可用)。用于find从数组中查找对象:


let result = mapsOrder.map(id =>

    mapData.find(o => o.id == id).gates.shift().coords

);

您可能想find在使用之前检查是否确实找到了某些东西,并且gates数组包含某些东西shift,这是一种更安全的方法:


let result = mapsOrder.map(id => {

    let obj = mapData.find(o => o.id == id);

    if(obj && obj.gates.length) {                  // if we found an object with the same id and that object still have gates

        return obj.gates.shift().coords;           // return the coords of the first gate and remove the gate from the array

    }                                              // otherwise, throw an error or something

});

不可更改:


而不是使用shift前面的示例,我们将使用一个对象来跟踪gates数组中的门索引:


let nextGateIndex = Object.create(null);                             // create a prototypeless object to track the next gate index for each object

let result = mapsOrder.map(id => {

    let obj = mapData.find(o => o.id == id);

    let index;

    if(nextGateIndex[id] == undefined) {

        index = 0;

    } else {

        index = nextGateIndex[id] + 1;

    }

    nextGateIndex[id] = index;

    if(obj && index < obj.gates.length) {

        return obj.gates[index].coords;

    }                                                                // throw error or something

});


查看完整回答
反对 回复 2021-05-06
?
慕的地10843

TA贡献1785条经验 获得超8个赞

如果按照您的描述,您的循环应类似于。似乎您要使用id和toId使用数组索引。用对象替换数组可能是一个好主意。

for(let i = 0; i < mapsOrder.length; i++) {

  let nextMap;

  let currentMapId = mapsOrder[i];

  if(i === mapsOrder.length - 1) {

    nextMapId = mapsOrder[0]   

  } else {

    nextMapId = mapsOrder[i + 1];    

  }

  let filteredMapData = mapData.filter(f => f.id == currentMapId);

  let filteredGates = filteredMapData.length > 0 ? filteredMapData[0].gates.filter(f => f.toId == nextMapId) : [];


  console.log('Current map is: ', currentMapId, 'and the next map id is:', nextMapId, 'gates:', filteredGates.length == 0 ? "no gates": filteredGates[0].coords)

  console.log('break----')


}


查看完整回答
反对 回复 2021-05-06
  • 3 回答
  • 0 关注
  • 153 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信