save data in an array with js

1

I have two variables with their respective values:

lat = ["-100.41812896728515","-100.41812896728517","-100.41812896728519"] lng = ["20.62346622550882","20.623466225508828","20.623466225508828"]

What I want is to unite both that is of the following form:

nuevo_array = [
["-100.41812896728515","20.62346622550882"],
["-100.41812896728517","20.623466225508828"],
["-100.41812896728519","20.623466225508828"],
]

So far I have the following:

 cords = [];
//  console.log("coord : " + typeof(lat));
for (let index = 0; index < lat.length; index++) {
    cords.push([parseFloat(lat[index]), '']);
    for (let index2 = 0; index2 < lng.length; index2++) {
        cords.push([cords[index], parseFloat(lng[index2])])
    }
}

What would be the proper way to achieve this?

    
asked by Eze 27.12.2018 в 18:17
source

1 answer

1

You do not need two loops, you can do it like this:

lat = ["-100.41812896728515","-100.41812896728517","-100.41812896728519"]
 lng = ["20.62346622550882","20.623466225508828","20.623466225508828"]

 cords = [];
//  console.log("coord : " + typeof(lat));
for (let index = 0; index < lat.length; index++) {
    cords.push([parseFloat(lat[index]), parseFloat(lng[index])]);    
}

console.log(cords);
    
answered by 27.12.2018 / 18:23
source