简体   繁体   English

三维Javascript数组

[英]Three dimensional Javascript array

I am trying to build a 3D Javascript array, but I am unsure of how to do it, basically I have 3 arrays, Provinces, Cities and Malls all in succession, so I want to create a 3D array to store all the data in and then write some jQuery/Javascript to get out what I need. 我正在尝试构建3D Javascript数组,但是我不确定该怎么做,基本上我有3个数组,依次是各省,城市和购物中心,因此我想创建一个3D数组以存储所有数据。然后编写一些jQuery / Javascript来了解我的需求。

I have a script that can populate a drop down list with array items, but now I am adding an extra dimension to it and I am getting a little confused as to how to proceed, here is the code I have thus far, 我有一个脚本,可以用数组项填充下拉列表,但是现在我在其中添加了一个额外的维度,并且我对如何继续感到有些困惑,这是到目前为止的代码,

The jQuery: jQuery:

<script>
    model[0] = new Array( 'Arnage', 'Azure', 'Brooklands', 'Continental', 'Corniche', 'Eight', 'Mulsanne', 'Series II', 'Turbo R', 'Turbo RT');
    model[1] = new Array( '412', '603', 'Beaufighter', 'Blenheim', 'Brigand', 'Britannia');
    model[2] = new Array( 'Inyathi', 'Rhino');
    model[3] = new Array( 'Amandla', 'Auto Union', 'Horch');

            function makeModel(obj){

            var curSel=obj.options[obj.selectedIndex].value ;
            var x;

            if (curSel != 'null'){


                $('#model').css({'display' : 'block'});

                $('#model').html("<select name='model' id='sub'>");
                for (x in model[curSel])
                {

                    $('#sub').append("<option value='" + model[curSel][x] + "'>" + model[curSel][x] + "</option>");
                }

            }else{
                $('#model').css({'display' : 'block'});
            }
        }

</script>

The HTML: HTML:

<form>
<p>
<span class='left'><label for='make'>Make: </label></span>
<span class='right'><select name='make' id='make' onchange='makeModel(this);'>
<option value='0'>Select one</option>
<option value='1'>one</option>
<option value='2'>two</option>
<option value='3'>three</option>
<option value='4'>four</option>
</select>
</span>
</p>
<p>
<div id='model'></div>
</p>
</form>

So as you can see, the above code generates a drop down menu of models depending on what make I select, now what I want to achieve now is adding one more level to it, so they will click on a province, then the cities drop down will appear, and when they choose a city, the malls will appear. 如您所见,上面的代码根据我选择的内容生成了一个模型下拉菜单,现在我想要实现的是在其中添加一个级别,以便他们单击一个省,然后将城市拖放到向下将出现,并且当他们选择城市时,将出现购物中心。

What would be the best way of approaching this? 解决这个问题的最佳方法是什么?

Thanx in advance! 提前感谢!

If you have a structure like this one 如果你有这样的结构

var provinces = [
   { name: "Province A", cities: [
        { name: "City A.A", malls: [
           { name: "Mall A.A.1" },
           { name: "Mall A.A.2" }
        ] },
        { name: "City A.B", malls: [
           { name: "Mall A.B.1" }
        ] }
   ] },
   { name: "Province B", cities: [
        { name: "City B.A", malls: [
           { name: "Mall B.A.1" },
           { name: "Mall B.A.2" }
        ] },
        { name: "City B.B", malls: [] }
   ] }
];

Then you can populate the dropdowns like so: 然后,您可以像这样填充下拉列表:

function populateDropdown(drop, items) {
   drop.empty();
   for(var i = 0; i < items.length; i++) {
      drop.append('<option value=' + i + '>' + items[i].name + '</option>');
   }
   drop.show();
}

populateDropdown( $('#provinces'), provinces );

And upon an action: 并采取行动:

$('#provinces').change(function() {
   var provinceIndex = $(this).val();
   var province = provinces[provinceIndex];

   populateDropdown( $('#cities'), province.cities );

   $('#malls').hide();
});

$('#cities').change(function() {
   var provinceIndex = $('#provinces').val();
   var province = provinces[provinceIndex];

   var cityIndex = $(this).val();
   var city = province.cities[cityIndex];

   populateDropdown( $('#malls'), city.malls );
});

EDIT 编辑

If the data structure on top looks hard to read, by the way, it's the exact same thing as the following: 顺便说一句,如果最上面的数据结构看起来很难阅读,则与以下内容完全相同:

var provinces = [];

// populate provinces
provinces.push({ name: "Province A", cities: [] });
provinces.push({ name: "Province B", cities: [] });

// populate cities
provinces[0].cities.push({ name: "City A.A", malls: [] });
provinces[0].cities.push({ name: "City A.B", malls: [] });
provinces[1].cities.push({ name: "City B.A", malls: [] });
provinces[1].cities.push({ name: "City B.B", malls: [] });

// populate malls
provinces[0].cities[0].malls.push({ name: "Mall A.A.1" });
provinces[0].cities[0].malls.push({ name: "Mall A.A.2" });
provinces[0].cities[1].malls.push({ name: "Mall A.B.1" });
provinces[1].cities[0].malls.push({ name: "Mall B.A.1" });
provinces[1].cities[0].malls.push({ name: "Mall B.A.2" });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM