Home > Mobile >  Efficient way to store information in Javascript
Efficient way to store information in Javascript

Time:08-06

I need to store inside my Java Script code some coordinates. At first there were just 10 or 20 coordinates, however now there are like 50 and it starts to be really messy. Inside my code i stored the information as a list:

function mia_posizione(position) {
    
    let latitudini = [45.830527, 45.879442, 46.017065, 46.045482, 46.025059, 45.980489, 46.029090, 46.084223, 45.836633, 45.837948, 45.837878, 45.834405, 45.836208, 45.832634, 46.481307];
    let longitudini = [9.029344, 8.979577, 8.931969, 8.978964, 8.965355, 8.926994, 8.969981, 9.036319, 9.032914, 9.026279, 9.023992, 9.035057, 9.034544, 9.021575, 9.916223];
    for(let i=0; i<latitudini.length; i  ){
        var latLocation =  latitudini[i];
        var lonLocation = longitudini[i];
        var latUser = position.coords.latitude;
        var lonUser = position.coords.longitude;
        var R = 6371;
        var dLat = deg2rad(latLocation - latUser);
        var dLon = deg2rad(lonLocation - lonUser);
        var a = Math.sin(dLat/2) * Math.sin(dLat/2)   Math.cos(deg2rad(latUser)) * Math.cos(deg2rad(latLocation)) * Math.sin(dLon/2) * Math.sin(dLon/2); 
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
        var y = R * c;
        var Approssimazione = aprox(y)
        document.getElementById(i).setAttribute("data-index", y);
        const Raggio = document.getElementById(i);
        if(Raggio.dataset.index>1000){
            document.getElementById(i).style.display="none"; 
        }
    }
}

The coordinates you see are just some of the many. Is there a way I can easily store and have access to them?

CodePudding user response:

you can add them in a js file where you define the data for example coordinates.js

var coordinates = [
     {latitudini: 45.830527, longitudini: 9.029344},
     {latitudini: 45.879442, longitudini: 8.979577},
];

and in html just include this file when ever you want to use this info

<script src="coordinates.js"></script>

now your code will look like so

function mia_posizione(position) {
    for(let i=0; i< coordinates.length; i  ){
        var latLocation =  coordinates[i].latitudini;
        var lonLocation = coordinates[i].longitudini;
        var latUser = position.coords.latitude;
        var lonUser = position.coords.longitude;
        var R = 6371;
        var dLat = deg2rad(latLocation - latUser);
        var dLon = deg2rad(lonLocation - lonUser);
        var a = Math.sin(dLat/2) * Math.sin(dLat/2)   Math.cos(deg2rad(latUser)) * Math.cos(deg2rad(latLocation)) * Math.sin(dLon/2) * Math.sin(dLon/2); 
        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
        var y = R * c;
        var Approssimazione = aprox(y)
        document.getElementById(i).setAttribute("data-index", y);
        const Raggio = document.getElementById(i);
        if(Raggio.dataset.index>1000){
            document.getElementById(i).style.display="none"; 
        }
    }
}

CodePudding user response:

Maybe you can try to format it or add spaces in your list so it's more readable. This is what I did. Let me know what you think.

function mia_posizione(position) {

let latitudini = [45.830527, 45.879442, 46.017065, 46.045482,
                  46.025059, 45.980489, 46.029090, 46.084223,
                  45.836633, 45.837948, 45.837878, 45.834405,
                  45.836208, 45.832634, 46.481307];

let longitudini = [9.029344, 8.979577, 8.931969, 8.978964,
                   8.965355, 8.926994, 8.969981, 9.036319,
                   9.032914, 9.026279, 9.023992, 9.035057,
                   9.034544, 9.021575, 9.916223];
for(let i=0; i<latitudini.length; i  ){
    var latLocation =  latitudini[i];
    var lonLocation = longitudini[i];
    var latUser = position.coords.latitude;
    var lonUser = position.coords.longitude;
    var R = 6371;
    var dLat = deg2rad(latLocation - latUser);
    var dLon = deg2rad(lonLocation - lonUser);
    var a = Math.sin(dLat/2) * Math.sin(dLat/2)   Math.cos(deg2rad(latUser)) * Math.cos(deg2rad(latLocation)) * Math.sin(dLon/2) * Math.sin(dLon/2); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var y = R * c;
    var Approssimazione = aprox(y)
    document.getElementById(i).setAttribute("data-index", y);
    const Raggio = document.getElementById(i);
    if(Raggio.dataset.index>1000){
        document.getElementById(i).style.display="none"; 
    }
}

}

  • Related