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

102
Views
For loop solo muestra el último valor en la matriz

Estoy tratando de enumerar un conjunto de valores, que solo muestran los nuevos valores actualizados en una matriz, pero desafortunadamente solo está imprimiendo el último valor en la matriz. ¿Cómo muestro todos los valores y no solo el último?

Cuando consuelo. Salgo de longLat[itemIndex], esto es lo que veo. Deben estar en la misma matriz.

Contraído:

ingrese la descripción de la imagen aquí

Expandido:

ingrese la descripción de la imagen aquí

Mi código -

 let longLat = [] for (let index of this.overwatchTargets) { let coordsArr = [index["longitude"], index["latitude"], index["userName"], index["dateTimeCaptured"]] longLat.push(coordsArr) for (let itemIndex in longLat) { // Update location coords this.map.getSource('data-source').setData({ "type": "FeatureCollection", "features": [{ "type": "Feature", "properties": { 'description': "Dispatcher: " + longLat[itemIndex][2] + "\n" + "\Lng/Lat\:" + longLat[itemIndex][0] + " / " + longLat[itemIndex][1] + "\n" + "\StartTime\: " + longLat[itemIndex][3] }, "geometry": { "type": "Point", "coordinates": longLat[itemIndex] <--- This should show all elements in the array, but only shows the last one } }] }); } }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Está pasando una matriz de un solo elemento varias veces en lugar de pasar una sola colección de coordenadas.

Intenta crear un método de

 indexToFeature(index) { const {longitude, latitude, userName, dateTimeCaptured} = index; return { "type": "Feature", "properties": { 'description': "Dispatcher: " + userName + "\n" + "\Lng/Lat\:" + longitude + " / " + latitude + "\n" + "\StartTime\: " + dateTimeCaptured }, "geometry": { "type": "Point", "coordinates": [longitude, latitude, userName, dateTimeCaptured] } }; }

Entonces su cuerpo principal puede ser simplemente

 this.map.getSource('data-source').setData({ "type": "FeatureCollection", "features": this.overwatchTargets.map(indexToFeature) });

para llamar a setData una vez, convirtiendo toda la matriz de overwatchTargets en una matriz de características.

about 4 years ago · Juan Pablo Isaza Report

0

Creo que separar estos dos bucles for ayudará

 let longLat = [] for (let index of this.overwatchTargets) { let coordsArr = [index["longitude"], index["latitude"], index["userName"], index["dateTimeCaptured"]] longLat.push(coordsArr) } for (let itemIndex in longLat) { // Update location coords this.map.getSource('data-source').setData({ "type": "FeatureCollection", "features": [{ "type": "Feature", "properties": { 'description': "Dispatcher: " + longLat[itemIndex][2] + "\n" + "\Lng/Lat\:" + longLat[itemIndex][0] + " / " + longLat[itemIndex][1] + "\n" + "\StartTime\: " + longLat[itemIndex][3] }, "geometry": { "type": "Point", "coordinates": longLat[itemIndex] } }] }); }
about 4 years ago · Juan Pablo Isaza Report
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!