如何使用javascript创建这样的数组
使用javascript,我想创建一个具有这种结构的数组,这样它就可以拥有country数组中的所有状态。每个状态都是一个数组,包含其中的所有区域。每个地区都有所有的健身俱乐部。大概是这样的:如何使用javascript创建这样的数组,javascript,Javascript,使用javascript,我想创建一个具有这种结构的数组,这样它就可以拥有country数组中的所有状态。每个状态都是一个数组,包含其中的所有区域。每个地区都有所有的健身俱乐部。大概是这样的: [Australia]=>array([NSW]=>array([Sydney]=>array(element1, element2, ...))); var countries = [ { name: "Australia", states:
[Australia]=>array([NSW]=>array([Sydney]=>array(element1, element2, ...)));
var countries = [
{
name: "Australia",
states: [
{
name: "state1",
regions: [
{
name: "region1",
healthclubs: [
"healthclub1", "healthclub2"
]
},
{
name: "region2",
healthclubs: [
"healthclub1", "healthclub2"
]
}
]
},
{
name: "state2",
regions: [
{
name: "region1",
healthclubs: [
"healthclub1", "healthclub2"
]
}
]
}
]
},
{
name: "USA"
}
];
到目前为止,我已经尝试了很多方法,包括下面的代码:
$('#search-widget-data li').each(function(){
var dataElement = $(this);
searchWidgetData.push(dataElement.text());
alldata.push([dataElement.prevAll(".state:first").text(),
dataElement.prevAll(".region:first").text());
});
您可以使用如下表单:
[Australia]=>array([NSW]=>array([Sydney]=>array(element1, element2, ...)));
var countries = [
{
name: "Australia",
states: [
{
name: "state1",
regions: [
{
name: "region1",
healthclubs: [
"healthclub1", "healthclub2"
]
},
{
name: "region2",
healthclubs: [
"healthclub1", "healthclub2"
]
}
]
},
{
name: "state2",
regions: [
{
name: "region1",
healthclubs: [
"healthclub1", "healthclub2"
]
}
]
}
]
},
{
name: "USA"
}
];
您可能希望以便于存储和作为JavaScript对象使用的格式存储数据结构。您可以尝试使用此格式。
变量Regar[n1][n2];
但是您需要指定每个内部数组的长度 你尝试过什么吗?我有一个很大的列表,从中我可以得到原始元素列表,我尝试通过获取列表中以前的州/地区/俱乐部来推动事情的发展