首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >三维数组解决方案javascript

三维数组解决方案javascript
EN

Stack Overflow用户
提问于 2015-01-20 17:09:36
回答 2查看 75关注 0票数 0

我正在从一个数据库中读取信息,我想要下列结构:大陆->乡村->城市。我在整个PHP中获取数据。我希望将这些数据存储在一个三维javascript数组中(或者至少从昨天开始我一直在尝试这样做)。

我不知道以下内容是否可行:

代码语言:javascript
复制
var triArray[0] = ["Africa"];
var triArray[0][0] = ["Niger"];
var triArray[0][0][0] = ["Abuya"];

这样做的目的是通过PHP生成这些数组,并使用数据来填充它们。

我“需要”(我想,我不是专家)一个三维的,然后看看哪个城市属于哪个国家和国家在哪里,使用一个for循环。

代码语言:javascript
复制
<ul
   <li>Africa
       <ul>
          <li>Niger
              <ul>
                  <li>Abuya</li>
              </ul>
          <li>
       </ul>
   </li>
</ul>

我不知道你是否得到了我想要的,谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-20 17:21:17

考虑一种更面向对象的方法--这应该很容易,因为大陆/国家/城市是有意义的。

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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作为一个整体容器。

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

代码语言:javascript
复制
// ------------------ 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;

票数 2
EN

Stack Overflow用户

发布于 2015-01-20 17:24:41

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

你应该选择这样的结构:

代码语言:javascript
复制
{
     continents : [

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

你可以这样访问它:

代码语言:javascript
复制
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知识和它的字符串表示将不同于一个简单的数组:

代码语言: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]);

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28051201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档