我正在从一个数据库中读取信息,我想要下列结构:大陆->乡村->城市。我在整个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>我不知道你是否得到了我想要的,谢谢。
发布于 2015-01-20 17:21:17
考虑一种更面向对象的方法--这应该很容易,因为大陆/国家/城市是有意义的。
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 Country和function 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;
}向Country和City添加类似的函数。
现在,您可以输出非洲大陆,比如在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;
发布于 2015-01-20 17:24:41
这是不可能的,如果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]);
https://stackoverflow.com/questions/28051201
复制相似问题