Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
¿Cómo ejecutar una función para cada índice en una matriz?

Tengo una función (que actualmente) debido a un cierre recordará el último índice que devolvió (de una matriz) en una llamada de función y devolverá el siguiente valor del siguiente índice.

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

Lo que estoy haciendo es pasar a la función una matriz de grados de un círculo:

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

Lo que me gustaría es pasar en otra matriz que es un rango de distancias de metros:

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

Lo que quiero es que el valor de grado actual se incremente cuando hayamos regresado y hayamos aplicado cada distancia a cada grado.

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

Entonces el grado debería comportarse como:

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

Lo intenté:

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

Pero eso no funcionó. ¡Cualquier ayuda sería apreciada!

ACTUALIZAR Podría ser útil para ver el contexto de lo que esto eventualmente hará:

Tengo marcadores que me gustaría agregar a un círculo en un mapa desde el centro hacia afuera. Los marcadores deben colocarse a lo largo de los grados que yo elija:

 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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!