O
O
Oufinx2018-03-19 10:08:42
JavaScript
Oufinx, 2018-03-19 10:08:42

How to make a multidimensional array a loop?

Good day.

Can you please tell me how to create such an array using a loop in JS?:

var locations = [
      ['1', -35.165034, 174.162854],
      ['2', -35.696121, 174.300132],
      ['3', -36.113032, 174.559536],
      ['4', -36.852924, 174.750234],
      ['5', -36.868273, 174.711450]
    ];


I'm trying to do this, but it doesn't come out right:

var Atext = ["Киев, Крещатик 44", "Киев, Перова 40", "Киев, Лаврухина 4"];
  var i = Atext.length;
  var j = 0;
  var Tarray = [];

while(j<i){
  $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address='+Atext[j]+'&key=AIzaSyCMDuMVwIsWG5q_HEG57iIbodBFhau0R_0', function(data) {
        var Alat = data['results'][0]['geometry']['location']['lat'];
        var Alng = data['results'][0]['geometry']['location']['lng'];

        Tarray.push([j+"",Alat,Alng]);        
  });
 j++;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Simonov, 2018-03-19
@SimasikS

var will "collapse" the value, in short, you will have j not 1,2,3,4,5 for different requests, but 5,5,5,5,5, you can look at let and replace var with let
Well, if you are allowed ES -2015
Read about let

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question