亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

通過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 關注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號