來自 console.log 的影像
大家好,我是 React JS 的新手,基本上是 Web 開發本身。有誰知道如何訪問嵌套物件的突出顯示部分?例如,我已經訪問了一些部分:我已經使用:props.list.name 訪問了名稱但是當涉及到 constellations 部分時,我很難訪問它并顯示它。順便說一句,這里是 API 鏈接https://api.genshin.dev/characters/eula 提前謝謝你:)
uj5u.com熱心網友回復:
您可以使用 map,通過陣列“星座”中的所有元素,
不知道你的代碼是什么樣子的,最好放你自己的代碼,編輯你的帖子,因為你的代碼不存在,我給你舉個簡單的例子
const constellations = [
{
id: 1,
name: "Dashboard",
description:"icon",
},
{
id: 2,
name: "Dashboard",
description:"icon",
}]
{constellations.map(constellation =>(
<div key={constellation.id}>
<h1>{constellation.name}<h1/>
</div>
))}
如果您使用“props.list”,您可以將代碼撰寫為:
{props?.list?.constellations?.map(constellation =>(
<div key={constellation.id}>
<h1>{constellation.name}</h1>
<h1>{constellation.unlock}</h1>
<h1>{constellation.description}</h1>
</div>
))}
uj5u.com熱心網友回復:
props.list.constellations[你的索引].name
uj5u.com熱心網友回復:
您可以對多級嵌套物件使用物件解構。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/460464.html
