gpt4 book ai didi

javascript - 如何在 React.js 中映射到另一个 map 内

转载 作者:行者123 更新时间:2023-12-01 00:58:27 25 4
gpt4 key购买 nike

使用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
}
}
}
}

这是我的 map 功能:

  {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>
))}

结果按预期显示在控制台中,但我也收到了许多其他未定义的结果。我认为这是 map 循环的问题,但我不知道我的问题到底在哪里:

这是我在控制台中找到的:

Console

enter image description here

最佳答案

这可以简化为

{
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>
);
})
}

并且使用索引作为键并不是一个好方法

希望这有帮助!!!

关于javascript - 如何在 React.js 中映射到另一个 map 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56326577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com