亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我不知道在這種情況下如何使用地圖

我不知道在這種情況下如何使用地圖

肥皂起泡泡 2021-12-23 16:13:31
我將這個 JSON 的內容寫入數組“heroes”,但我不知道如何獲取“damage”、“basicAttack.category”和“specialAttack.category”進行映射。我試圖解決這個問題,但不幸的是我不知道如何解決。{    "id": 20,    "name": "Warrior",    "skills": [        {            "id": 1,            "basicAttack": {                "id": 2,                "name": "Hit1",                "category": "weakAttack"            },            "specialAttack": {                "id": 16,                "name": "Special1",                "category": "spellAttack"            },            "damage": 200        },        {            "id": 2,            "basicAttack": {                "id": 3,                "name": "Hit2",                "category": "rangeAttack"            },            "specialAttack": {                "id": 17,                "name": "Special2",                "category": "fightAttack"            },            "damage": 100        }    ]}這是我的映射方式以及我想要獲取的數據const item = this.state.heroes.skills.map(item => {    /*i dont have idea how map        <p>{item.damage}</p>        <p>{item.specialAttack.cathegory} + {item.basicAttack.category}</p>     */})
查看完整描述

3 回答

?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

只是地圖上的heroes.skills,你會發現結果如何訪問值


var heroes ={

  "id": 20,

  "name": "Warrior",

  "skills": [

      {

          "id": 1,

          "basicAttack": {

              "id": 2,

              "name": "Hit1",

              "category": "weakAttack"

          },

          "specialAttack": {

              "id": 16,

              "name": "Special1",

              "category": "spellAttack"

          },

          "damage": 200

      },

      {

          "id": 2,

          "basicAttack": {

              "id": 3,

              "name": "Hit2",

              "category": "rangeAttack"

          },

          "specialAttack": {

              "id": 17,

              "name": "Special2",

              "category": "fightAttack"

          },

          "damage": 100

      }

  ]

}


heroes.skills.map(hero=>{

  console.log("damage...........",hero.damage)

  console.log("basicAttack.category.........",hero.basicAttack.category)

  console.log("specialAttack.category........",hero.specialAttack.category)

})

如果你想渲染,那么你必須返回它然后渲染


const heroesDiv = this.state.heroes.skills.map((hero) => (

  <>

    <p>{item.damage}</p>

    <p>{item.specialAttack.category} + {item.basicAttack.category}</p>

  </>

))


查看完整回答
反對 回復 2021-12-23
?
白衣非少年

TA貢獻1155條經驗 獲得超0個贊

this.state.heroes.skills.map((item) => {

  return `<div><p>${item.damage}</p><p>${item.specialAttack.category} ${item.basicAttack.category}</p></div>`

})


查看完整回答
反對 回復 2021-12-23
?
拉風的咖菲貓

TA貢獻1995條經驗 獲得超2個贊

let data = {

  "id": 20,

  "name": "Warrior",

  "skills": [

      {

          "id": 1,

          "basicAttack": {

              "id": 2,

              "name": "Hit1",

              "category": "weakAttack"

          },

          "specialAttack": {

              "id": 16,

              "name": "Special1",

              "category": "spellAttack"

          },

          "damage": 200

      },

      {

          "id": 2,

          "basicAttack": {

              "id": 3,

              "name": "Hit2",

              "category": "rangeAttack"

          },

          "specialAttack": {

              "id": 17,

              "name": "Special2",

              "category": "fightAttack"

          },

          "damage": 100

      }

  ]

}

這是你可以映射的方式


return (

 <div>

  {data.skills.map(item=>

  <div key={item.id}>

   <h3>{item.id}: {item.basicAttack.name}-{item.basicAttack.category}, {item.specialAttack.name}-{item.specialAttack.category}</h3>

  </div>)}

 </div>

)


查看完整回答
反對 回復 2021-12-23
  • 3 回答
  • 0 關注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號