带有字段和子对象数组的JavaScript对象数组?

时间:2017-09-15 13:59:41

标签: javascript arrays

为什么这不是有效的JavaScript?

我有一个数组。数组中的每个项都有一个标题字段,还有一个子数组。子数组具有包含img字段的对象,有时也包含内容字段。

const content = [
  {
    "title": "First title",
    [
      {"img": img1, "content": "This is content"},
      {"img": img2},
      {"img": img3}
    ]
  },
  {
    "title": "Second title",
    [
      {"img": img4, "content": "This is also content"},
      {"img": img5},
      {"img": img6}
    ]
  }
];

1 个答案:

答案 0 :(得分:1)

嵌套数组没有属性名称。

const content = [
{
    "title": "First title",
    "missingKey1" : [
      {"img": img1, "content": "This is content"},
      {"img": img2},
      {"img": img3}
    ]
  },
  {
    "title": "Second title",
    "missingKey2" : [
      {"img": img4, "content": "This is also content"},
      {"img": img5},
      {"img": img6}
    ]
  }
];