【发布时间】:2021-12-04 22:31:29
【问题描述】:
如何迭代对象及其子对象的属性?我用过 in 但我无法获得歌曲信息:/
let discos = [];
let disco1 = {
discoName: 'name disco1',
band: 'band name',
code: 1,
songs: [
{ 'songName': 'Song Name',
'duration': 200,
},
],
};
let disco2 = {
discoName: 'name disco 2',
band: 'band name 2',
code: 1,
songs: [
{ 'songName': 'Song Name 0',
'duration': 200,
},
],
};
discos.push(disco1,disco2);
for (let disco in discos){
console.log(discos[disco].discoName);
}
【问题讨论】:
-
discos是一个数组,因此您应该使用for .. of而不是for .. in进行迭代 -
此信息:歌曲:[ { 'songName': 'Song Name', 'duration': 200, }, ],
标签: javascript arrays loops