Javascript在尝试声明嵌套对象时失败

Javascript fails while trying to declare nested object

本文关键字:对象 失败 嵌套 声明 Javascript      更新时间:2023-09-26

我正在尝试构建一个基于jQuery的简单邮件模板系统。它基本上是一个嵌套数组,应该看起来像:

templates[1] = {
                    "name":"product damage claim",
                    "def":{
                        {'Customer Name?','delivery_name',1},
                        {'Date by which information should be provided by customer?','',1},
                        {'Order ID','orders_id',0}
                    },
                    "tpl":'Mail Content goes here'
};

现在,如果我写上面的内容,javascript就会失败。我在定义def对象时似乎做错了什么,知道吗?

由于def包含一个值列表,因此它应该是数组的数组

templates[1] = {
    "name": "product damage claim",
        "def": [
        ['Customer Name?', 'delivery_name', 1],
        ['Date by which information should be provided by customer?', '', 1],
        ['Order ID', 'orders_id', 0]
    ],
        "tpl": 'Mail Content goes here'
};

当您想要一个简单的值列表时,您需要一个数组:

"def": [
   ['Customer Name?','delivery_name',1],
   ['Date by which information should be provided by customer?','',1],
   ['Order ID','orders_id',0]
],

现在,这将解决您的语法问题,但这种排列并不能使提取存储在对象中的内容变得特别容易。