1

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?

6
  • 1) Don't stringify it. You can't iterate over a string (you can, but not how you want). 2) You need to map over the Object.values and extract the data you need and return a new array. Commented Jul 28, 2021 at 20:33
  • You can JSON.parse it and iterate through as an object. Commented Jul 28, 2021 at 20:34
  • @sidc I think it might already be an object Commented Jul 28, 2021 at 20:35
  • @evolutionxbox. Oh I thought he was getting JSON from an API or file, in which case you need to parse it. Commented Jul 28, 2021 at 20:36
  • There is no such thing as a JSON object. You either mean JSON (which is a string) or a JS object. Commented Jul 28, 2021 at 20:39

1 Answer 1

4

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>
   )
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I stop trying map when its a Object instead of Array. I feel so dumb now. Thank you so much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.