如何在数组中添加该对象?

时间:2018-07-11 06:11:29

标签: javascript html5

如何在数组中添加{[]}

screenshot1 screenshot2

2 个答案:

答案 0 :(得分:0)

@CertainPerformance是正确的。如果要具有对象,则必须具有关联的属性。

var a = [ { propertyName : [] } ]

然后您可以像这样访问该数组:

a[0].propertyNamea[0]['propertyName']

您也可以在对象内部包含多个值:

var a = [
          {
            propertyName_1 : [],
            propertyName_2 : "",
            propertyName_3 : 3,
          }
        ];

答案 1 :(得分:0)

var a = [{}] // no problem, you are assigning an empty object `{}` as first element of array
var a = [[]] // no problem, you are assigning an empty array `[]` as first element of array
var a = [{[]}] // Not working because you're assigning empty array into object
               //object needs key to store value

var a = {[]} //Not ok <<======== have you ever see var a = { 1, 2, 3} ?

请参阅documentation

  

对象是属性的集合,属性是名称(或键)与值之间的关联。