ReactJS map through Object

前端 未结 8 2109
别跟我提以往
别跟我提以往 2021-01-31 16:53

I have a response like this:

I want to display the name of each object inside this HTML:

{subjects.map((item, i) => (
  
  • 8条回答
    •  隐瞒了意图╮
      2021-01-31 17:16

      Do you get an error when you try to map through the object keys, or does it throw something else.

      Also note when you want to map through the keys you make sure to refer to the object keys correctly. Just like this:

      { Object.keys(subjects).map((item, i) => (
         
    • key: {i} Name: {subjects[item]}
    • ))}

      You need to use {subjects[item]} instead of {subjects[i]} because it refers to the keys of the object. If you look for subjects[i] you will get undefined.

    提交回复
    热议问题