JavaScript Determine next map by mapsOrder array

I have mapsOrder array and mapsData array of objects:

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
        }
      }
    ]
  }
]

What I want to achieve is in loop basing on mapsOrder where mapsOrder array values are ids in mapData, designate gates to next map.

So we have loop that iterate 4 times and when:

  • loop index is 1 current map is 1 next map is 2 and gates to next are coords: { x: 2, y: 42 }
  • loop index is 2 current map is 2 next map is 1 and gates to next are coords: { x: 6, y: 5 }
  • loop index is 3 current map is 1 next map is 3 and gates to next are coords: { x: 9, y: 4 }
  • loop index is 4 current map is 3 next map is 1 and gates to next are coords: { x: 2, y: 1 }

last loop iteration see next map as first of mapsOrder array. I tried to do it myself by first determineting the id of next map like so:

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-----')

}

but this console incorrect ids, demo

Answer:1

If follow your description your loop should look like. Seems that you wand to use id and toId but using array indexes. It can be a good idea to replace arrays with objects.

Demo

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----')

}
Answer:2

I am trying to submit a form and while submitting I call a function which is myOwnFun and it runs. But if I keep pressing enter for long time, form submit repeats which I don't want. Here is the ...

I am trying to submit a form and while submitting I call a function which is myOwnFun and it runs. But if I keep pressing enter for long time, form submit repeats which I don't want. Here is the ...

  1. javascript calling function only once

Maybe I am being stupid here but I cannot figure out how to respond to a Foundation 5 TopBar dropdown selection with a javascript function. I would like to select a theme for a library I use. To set ...

Maybe I am being stupid here but I cannot figure out how to respond to a Foundation 5 TopBar dropdown selection with a javascript function. I would like to select a theme for a library I use. To set ...

I created a class which sets up a pausable rxjs observable on an interval: export class RepeatingServiceCall<T> { private paused = false; private observable: Observable<T>; ...

I created a class which sets up a pausable rxjs observable on an interval: export class RepeatingServiceCall<T> { private paused = false; private observable: Observable<T>; ...

  1. restart timer rxjs

I'm trying to scrap some information from this ASP page http://laredoute.fr/ppdp/prod-350007615.aspx mainly the first 4 images in high resolution that load in the image-carousel. Depending on the ...

I'm trying to scrap some information from this ASP page http://laredoute.fr/ppdp/prod-350007615.aspx mainly the first 4 images in high resolution that load in the image-carousel. Depending on the ...

  1. scrapy javascript pagination
  2. scrapy javascript support
  3. scrapy javascript click
  4. scrapy javascript onclick
  5. scrapy javascript site
  6. scrapy javascript generated content
  7. scrapy javascript rendered pages
  8. scrapy javascript link
  9. scrapy javascript next page
  10. scrapy javascript python
  11. scrapy javascript example
  12. scrapy javascript selenium
  13. scrapy javascript website
  14. scrapy javascript variable
  15. scrapy javascript button
  16. scrapy javascript form
  17. scrapy javascript table
  18. scrapy javascript postback
  19. scrapy javascript ajax
  20. scrapy javascript text