0

In console I see key. But in browser I don’t see key, only element.title. Want to see in the browser:

key 1
title 1
title 2

key 2
title 3
title 4

{Object.entries(bob).map(([key, value]) => {
        return (
          console.log("key", key),
          (<Typography>{key}</Typography>),
          value.map((element) => (
            <Typography key={element.id}>{element.title}</Typography>
          ))
        )
      })}
2
  • 1
    If you want to see it in the browser then display it (just as you are displaying the title)? Commented Aug 22, 2022 at 16:15
  • OP needs to add the respective tag for React. This is not valid Javascript by itself. Commented Aug 22, 2022 at 17:38

1 Answer 1

1

I'm assuming bob looks something like this

const bob = {
  1: [{title: 1}, {title: 2}],
  2: [{title: 3}, {title: 4}]
}

You can try out the following snippet

{Object.entries(bob).map(([key, value]) => {
  console.log("key", key);
  return (
    <>
      <Typography>{key}</Typography>
      {value.map((element) => (
        <Typography key={element.id}>{element.title}</Typography>
      ))}
    </>
  )
})}

Notice the <></> surrounding the key and the value, its called React Fragments. Since you can't return more than one react node, you need to either surround the return with a valid HTML element/React component or use a fragment

Sign up to request clarification or add additional context in comments.

Comments

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.