Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
Create an associative array from for loop results in javascript

I have a form that calculates mileage from 4 destinations to one single origin. When submitted, I can display the text that shows the origin and the distance to each destination on a new line, for example:

Margate, UK to Coventry, UK: 177 mi in 3 hours 13 mins Brighton, UK to Coventry, UK: 158 mi in 2 hours 41 mins Bristol, UK to Coventry, UK: 108 mi in 1 hour 49 mins Birmingham, UK to Coventry, UK: 21.8 mi in 31 mins

I want to create an array of this information so I can call it in other functions. For example, I just want the Destination and the miles, so something like this:

{"Margate, UK" => 177, "Brighton, UK" => 158, "Bristol, UK" => 108, "Birmingham, UK" => 21.8}

Here is the code I use to make the for loop:

function calcDistance(response, status) {
  if (status != google.maps.DistanceMatrixStatus.OK) {
    alert('Error was: ' + status);
  } else {
    var origins = response.originAddresses;
    var destinations = response.destinationAddresses;

    for (var i = 0; i < origins.length; i++) {
      var results = response.rows[i].elements;
      for (var j = 0; j < results.length; j++) {

    outputDiv.innerHTML += origins[i] + ' to ' + destinations[j]
                + ': ' + results[j].distance.text + ' in '
                + results[j].duration.text + '<br>';

      }
    }
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can try making an array const distances = [] and then push into it the desired information in the inner for loop

...
var results = response.rows[i].elements;
for (var j = 0; j < results.length; j++) {
  distances.push([origins[i], results[j].distance.text])
  ...
}

or you could extract the information from the submitted form text using regex

const distinations =
  'Margate, UK to Coventry, UK: 177 mi in 3 hours 13 mins Brighton, UK to Coventry, UK: 158 mi in 2 hours 41 mins Bristol, UK to Coventry, UK: 108 mi in 1 hour 49 mins Birmingham, UK to Coventry, UK: 21.8 mi in 31 mins'

const cities = distinations.match(/\b(?:(?!Coventry)\w)\w+, \w+/g)
const distances = distinations.match(/(?<=: )\d+/g)

const result = []
for (let index in cities) {
  const city = cities[index]
  const distance = distances[index]
  result.push({
    city,
    distance
  })
}

console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda