Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

253
Visualizações
How to execute a function for every index in an array?

I have a function (which currently) because of a closure will remember the last index it returned (from an array) on a function call and return the next value from the next index.

  function getCurrentValue(values) {
    let index = -1;
    let l = values.length;

    function increment() {
      ++index;
      if (index < l) {
        return values[index];
      } else {
        index = -1;
        ++index;
        return values[index];
      }
    }

    return increment;
  }

What I am doing is I'm passing into the function an array of degrees of a circle:

  let degree = getCurrentValue([0, 315, 270, 225, 180, 135, 90];

  degree() // returns 0
  degree() // returns 315

What i'd like is to pass in another array which is a range of distances of meters:

distances = [75, 175, 275, 375, 475];

What I want is the current degree value to increment when we have returned have applied each distance to every degree.

 let degree = getCurrentValue([0, 315, 270, 225, 180, 135, 90], [75, 175]);

So degree should behave like:

 degree(); // returns 0;
 degree(); // returns 0;
 degree(); // returns 0;
 degree(); // returns 315;
 degree(); // returns 315;
 degree(); // returns 315;

I tried:

   function getCurrentValue(values, helper) {
    let index = -1;
    let l = values.length;
    var temp;

    function increment() {
      ++index;
      if (index < l) {
        return values[index]
      } else {
        index = -1;
        ++index;
        return values[index]
      }
    }
    
      if (typeof helper != 'undefined') {
        for (let i = 0; i < helper.length; i++) {
          console.log('temp', temp)
          return temp = values[index];
        }
      }

    return increment;
  }

But that didn't work. Any help would be appreciated!

UPDATE Might be helpful to see the context of what this will eventually do:

I have markers I'd like to add to a circle on a map from the center outwards. Thee markers should be laid down along degrees I choose:

 function setMarkerToCenterOfSegment(lat, lng, degree, distance) {
    return GeometryUtil.destination(L.latLng(lat, lng), degree(), distance());
  }

  let degree = getCurrentValue([0, 315, 270, 225, 180, 135, 90], [75, 175, 275, 375, 475]);
  let distance = getCurrentValue([75, 175, 275, 375, 475])

 {markers.length && markers.map((marker, index) => {
   return 
    <Marker
     key={index}
     position={setMarkerToCenterOfSegment(...initRadiusForCircle, degree, distance)} />
     }
  )}
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda