为什么这不是有效的 JavaScript?
我有一个数组。数组中的每个项目都有一个标题字段和一个子数组。子数组的对象包含 img 字段,有时也包含 content 字段。
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}
]
}
];
最佳答案
嵌套数组没有属性名称。
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}
]
}
];
关于带有字段和子对象数组的 JavaScript 对象数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46241405/