简体   繁体   中英

How Do i Iterate through a JSON object in ReactJS?

On my ReactJS file, I JSON.stringify my object to see what i get.

return ( 
    <div> 
        { JSON.stringify(peopleChannel) };
    </div>
)

I get return something like this

{
  "Y3WJb6": {
    "photoURL": "https://myimage.com",
    "email": "abc.com"
  },
  "Yzfd6": {
    "photoURL": "https://myimage23.com",
    "email": "adfasfd.com"
  }
}

How do I render it into like a list?

You can map over Object.values(peopleChannel), or if you need the object keys as well use Object.entries:

   return (
    <div>
    {Object.entries(peopleChannel).map(([id, {photoURL, email}]) => (
      <div>
        <div>{id}</div>
        <div>{photoURL}</div>
        <div>{email}</div>
      </div>
    ))}
    </div>
   )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM