Declare an empty two-dimensional array in Javascript?

后端 未结 18 1387
暖寄归人
暖寄归人 2020-11-29 21:45

I want to create a two dimensional array in Javascript where I\'m going to store coordinates (x,y). I don\'t know yet how many pairs of coordinates I will have because they

18条回答
  •  长情又很酷
    2020-11-29 22:42

    Create an object and push that object into an array

     var jSONdataHolder = function(country, lat, lon) {
    
        this.country = country;
        this.lat = lat;
        this.lon = lon;
    }
    
    var jSONholderArr = [];
    
    jSONholderArr.push(new jSONdataHolder("Sweden", "60", "17"));
    jSONholderArr.push(new jSONdataHolder("Portugal", "38", "9"));
    jSONholderArr.push(new jSONdataHolder("Brazil", "23", "-46"));
    
    var nObj = jSONholderArr.length;
    for (var i = 0; i < nObj; i++) {
       console.log(jSONholderArr[i].country + "; " + jSONholderArr[i].lat + "; " + 
       jSONholderArr[i].lon);
    
    }
    

提交回复
热议问题