三维数组解决方案javascript

时间:2015-01-20 17:09:36

标签: javascript

我正在从数据库中读取信息,我想要以下结构:continent-> Country-> City。我在整个PHP中获取数据。我想将这些数据存储在一个三维的javascript数组中(或者至少是我昨天尝试做的)。

我不知道以下是否可行:

var triArray[0] = ["Africa"];
var triArray[0][0] = ["Niger"];
var triArray[0][0][0] = ["Abuya"];

我们的想法是通过PHP制作这些数组并使用数据来填充它们。

我需要" (我认为,我不是和专家)一个三维的,然后使用for循环查看哪个城市属于哪个国家和该国家所在的位置。

<ul
   <li>Africa
       <ul>
          <li>Niger
              <ul>
                  <li>Abuya</li>
              </ul>
          <li>
       </ul>
   </li>
</ul>

我不知道你能得到我想要的东西,提前谢谢。

2 个答案:

答案 0 :(得分:2)

考虑采用更加面向对象的方法 - 这应该很容易,因为大陆/国家/城市都是事物。

function Continent(name) {
    this.name = name;
    this.countries = [];
}
Continent.prototype.addCountry = function(country) {
    if( !(country instanceof Country)) throw new Error("Not a country");
    this.countries.push(country);
    // may want to add logic for duplicate checking
}

您可以以类似的方式构建function Countryfunction City

现在已经完成了,你已经构建了你的结构,你可以输出它。也许是这样的:

Continent.prototype.toString = function() {
    var list = "<li>" + this.name,
        l = this.countries.length, i;
    if( l > 0) {
        list += "<ul>";
        for( i=0; i<l; i++) {
            list += this.countries[i]; // toString implicitly called
        }
        list += "</ul>";
    }
    list += "</li>";
    return list;
}

CountryCity添加类似的功能。

现在您可以输出大陆,例如someULelement.innerHTML,然后根据需要进行渲染。

您可能希望function World充当整体容器。

面向对象的代码可以使这样的任务更容易理解。

// ------------------ BEGIN CLASS DEFINITIONS ---------------------

function World() {
  this.continents = [];
}
World.prototype.addContinent = function(continent) {
  if (!(continent instanceof Continent)) throw new Error("Not a continent");
  this.continents.push(continent);
}
World.prototype.toString = function() {
  var list = "<ul>",
    l = this.continents.length,
    i;
  if (l > 0) {
    for (i = 0; i < l; i++) {
      list += this.continents[i]; // toString implicitly called
    }
  }
  list += "</ul>";
  return list;
}

function Continent(name) {
  this.name = name;
  this.countries = [];
}
Continent.prototype.addCountry = function(country) {
  if (!(country instanceof Country)) throw new Error("Not a country");
  this.countries.push(country);
  // may want to add logic for duplicate checking
}
Continent.prototype.toString = function() {
  var list = "<li>" + this.name,
    l = this.countries.length,
    i;
  if (l > 0) {
    list += "<ul>";
    for (i = 0; i < l; i++) {
      list += this.countries[i]; // toString implicitly called
    }
    list += "</ul>";
  }
  list += "</li>";
  return list;
}

function Country(name) {
  this.name = name;
  this.cities = [];
}
Country.prototype.addCity = function(city) {
  if (!(city instanceof City)) throw new Error("Not a city");
  this.cities.push(city);
}
Country.prototype.toString = function() {
  var list = "<li>" + this.name,
    l = this.cities.length,
    i;
  if (l > 0) {
    list += "<ul>";
    for (i = 0; i < l; i++) {
      list += this.cities[i]; // toString implicitly called
    }
    list += "</ul>";
  }
  list += "</li>";
  return list;
}

function City(name) {
  this.name = name;
}
City.prototype.toString = function() {
  return "<li>" + this.name + "</li>";
}

// ------------------ END CLASS DEFINITIONS ---------------------

var world = new World(),
  africa = new Continent('Africa'),
  niger = new Country('Niger'),
  abuya = new City('Abuya');

world.addContinent(africa);
africa.addCountry(niger);
niger.addCity(abuya);

document.body.innerHTML = world;

答案 1 :(得分:2)

这是不可能的,如果triArray[0]是一个字符串,则它不能是一个字符串数组。

你应该选择这样的结构:

{
     continents : [

         {
             name:"Africa",
             countries : [
                 {
                     name : "Niger",
                     cities : [
                         "Abuya" , "", ...
                     ]
                 },
                 ...
             ]
         },
         ...
     ]
 }

你可以像这样访问它:

var continents = data["continents"];

for (var i =0,len = continents.length; i<len;i++){
    var continentData = continents[i];

    var continentName = continentData["name"];

    var listCountries = continentData["countries"];
    for (var y =0,leny = listCountries.length; y<leny;y++){
        var countryData = listCountries[y];

        var countryName = countryData["name"];

        // List of cities
        var cities = countryData["cities"];

    }

}

编辑: 为信息添加,这是可能的,但暗示一些JavaScript知识和它的字符串表示将不同于一个简单的数组:

 
var world = [];
world.name = "world";

world.push([])

world[0].name = "Africa";
world[0].push([]);


world[0][0].name = "Niger";
world[0][0].push("Abuya");

document.write(world[0].name);
document.write(world[0][0].name);
document.write(world[0][0][0]);