在React.js中使用map函数从json中获取Mp3的内容:
{
"175": {
"label": "Pub Radio",
"icone": "",
"Mp3": {
"33278": {
"id": 33278,
"titre": "Ricardo Villalobos - Caminando",
"intention1": "Doux",
"intention2": "Doux",
"intention3": "Doux",
"langue": "Allemand",
"visibilite": 1
}
}
},
"176": {
"label": "Pub Cd/Dvd",
"icone": "",
"Mp3": {
"33277": {
"id": 33277,
"titre": "Mano lo taugh - Primative People",
"intention1": "Chaleureux, rassurant",
"intention2": "Joyeux",
"intention3": "Souriant",
"langue": "Allemand",
"visibilite": 1
},
"33279": {
"id": 33279,
"titre": "Foals - Late Night (Solomun Remix).mp3",
"intention1": "Amical, complice",
"intention2": "Amical, complice",
"intention3": "Amical, complice",
"langue": "Allemand",
"visibilite": 1
}
}
},
"245": {
"label": "Billboard",
"icone": "",
"Mp3": {
"33280": {
"id": 33280,
"titre": "Techno",
"intention1": "Posé, calme",
"intention2": "Amical, complice",
"intention3": "Souriant",
"langue": "Americain",
"visibilite": 1
}
}
}
}
这是我的地图功能:
{Object.keys(extraitMP3).map((label, i) => (
<li key={i}>
<span >key: {i} Name: {extraitMP3[label]}</span>
{Object.keys(extraitMP3[label]).map((Mp3, i) => (
<li key={i}>
<span >key: {i} Name: {extraitMP3[label][Mp3]}</span>
{Object.keys(extraitMP3[label][Mp3]).map((idSon, i) => (
<li key={i}>
<span >key: {i} Name: {extraitMP3[label][Mp3][idSon]}</span>
{console.log('Titre',extraitMP3[label][Mp3][idSon].titre)}
{console.log('Intention 1',extraitMP3[label][Mp3][idSon].intention1)}
{console.log('Intention 2',extraitMP3[label][Mp3][idSon].intention2)}
{console.log('Intention 3',extraitMP3[label][Mp3][idSon].intention3)}
{console.log('Langue',extraitMP3[label][Mp3][idSon].langue)}
{console.log('Visibilite',extraitMP3[label][Mp3][idSon].visibilite)}
</li>
))}
</li>
))}
</li>
))}
结果在控制台中按预期显示,但是我还收到许多其他未定义的结果。我认为这是循环使用地图的问题,但我不知道我的问题到底在哪里:
这是我在控制台中找到的内容:
答案 0 :(得分:1)
简短回答:
extraitMP3[label][Mp3]
应该为extraitMP3[label].Mp3
(或者为extraitMP3[label]['Mp3']
,尽管大多数短毛猫都不推荐使用
但是有几处错误
<li key={i}>
放置唯一标识符,例如歌曲ID(用于最后一个嵌套循环)答案 1 :(得分:1)
这可以简化为
{
Object.keys(extraitMP3).map(ids => {
return (
<li key={ids}>
<span >key: {ids} Name: {extraitMP3[ids].label}</span>
{
Object.keys(extraitMP3[ids].Mp3).map(idJson => {
return(
<li key={idJson}>
<span>key: {idJson} Name: {idJson}</span>
{console.log('Titre',extraitMP3[ids].Mp3[idJson].titre)}
{console.log('Intention1',extraitMP3[ids].Mp3[idJson].intention1)}
{console.log('Intention 2',extraitMP3[ids].Mp3[idJson].intention2)}
{console.log('Intention 3',extraitMP3[ids].Mp3[idJson].intention3)}
{console.log('Langue',extraitMP3[ids].Mp3[idJson].langue)}
{console.log('Visibilite',extraitMP3[ids].Mp3[idJson].visibilite)}
</li>
);
})
}
</li>
);
})
}
并且使用索引作为键不是一个好方法
希望这会有所帮助!