关联对象数组javascript

时间:2014-07-11 13:25:42

标签: javascript

如何在js中声明/硬编码以下字典?

var rooms = new Dictionary<string, List<Room>>
{
    {
        "Single room", new List<Room>
        {
            new Room
            {
                Caption = "Single room1",
                Price = "12.--"
            },
            new Room()
            {
                Caption = "Single room2",
                Price = "12.--"
            }
        }
    },
    {
        "Double room", new List<Room>
        {
            new Room
            {
                Caption = "Double room1",
                Price = "12.--"
            },
            new Room()
            {
                Caption = "Double room2",
                Price = "12.--"
            }
        }
    }
};

看来这个样本太长了,所以我在这里添加了一些文字。这应该不会太难,我只是无法绕过它,这是一个如此简洁的问题,我虽然在这里发布。

2 个答案:

答案 0 :(得分:1)

将数组用于列表,类的对象:

var rooms = {

    "Single room": [
        {
            Caption: "Single room1",
            Price: "12.--"
        },
        {
            Caption: "Single room2",
            Price: "12.--"
        }
    ],
    "Double room": [
        {
            Caption: "Double room1",
            Price: "12.--"
        },
        {
            Caption: "Double room2",
            Price: "12.--"
        }
    ]
};

答案 1 :(得分:0)

这样的事情:

var rooms = {
    'Single room': [
        { 'caption': 'Single room1', 'price': '12.--' }
        { 'caption': 'Single room2', '12.--' }
    ],
    'Double room': [
        { 'caption': 'Double room1', 'price': '12.--' }
        { 'caption': 'Double room2', '12.--' }
    ]
};