【发布时间】:2023-01-18 04:30:17
【问题描述】:
const module = {
video: [
{ id: 1, title: 'video', component: <Video />},
],
condensed: [
{ id: 2, title: 'condensed', component: <Condensed /> },
],
full: [
{ id: 3, title: 'full', component: <Full /> },
],
};
有没有办法循环遍历其中数组被命名的对象数组?希望我使用正确的语言来描述这一点。 我正在为每个数组打印出 id 和 title。
如果数据看起来像这样,我相信我可以使用 for 循环(但我意识到我可以使用 forEach 或 map):
const module = {
video: [
{ id: 1, title: 'video', component: <Video />},
{ id: 2, title: 'condensed', component: <Condensed /> },
],
for (var key in module.video) {
var obj = module.video[key];
// ...
}
【问题讨论】:
-
数组在对象中并没有什么特别之处。只需像使用任何其他包含数组的变量一样使用
module.video。 -
component键中的那些字符串? -
在这种情况下,键不代表视频中的每个对象吗?
obj.id和obj.title行不通吗?
标签: javascript arrays object