如何在extjs中为json创建嵌套组合框的数组

时间:2014-05-16 12:58:02

标签: javascript arrays json extjs combobox

如何在extjs中从json创建数组。请在下面找到json结构和所需的数组结构

 "DepartmantCodes": [
                    {
                        "DepartmentCode": "12",
                        "DivisionCode": [
                            "11",
                            "22"
                        ]
                    },
                    {
                        "DepartmentCode": "22",
                        "DivisionCode": [
                            "21",
                            "23"
                        ]
                    }
                ]
Array structure 
    [
       ['12','11'],
       ['12','22'],
       ['22','21'],
       ['22','23'],


    ]

1 个答案:

答案 0 :(得分:2)

使用Ext.each和一个空数组,您可以遍历json对象并创建所需的数组:

        var endArray = [];
        Ext.each(departmentCodes,function(departmentCode){
            Ext.each(departmentCode.DivisionCode,function(divisionCode){
                endArray.push([departmentCode.DepartmentCode,divisionCode]);
            });
        });

我在示例中对foreach进行了双重嵌套,因为虽然你的代码在每个数组中只有2个分区代码,但我认为可以有任意数量的分区代码?

这是fiddle for a working demonstration