1

I couldn't really find this anywhere else so here we go. I'm trying to map through a nested object and display the values but all I can get is the key displayed.

Object:

data = {
  objectOne: {
    name: "some name",
    otherValue: "something else"
  },
  someValue: "someValue",
  someOtherValue: "asdasd",
  objectTwo : {
    v1 : "v1",
    v2 : "v2",
    v3 : "v3",
  }
}

My function to loop through it: (I only want to display the content of objectOne)

Object.keys(data.objectOne).map(field => <div key={field}>{field}</div>

This returns name and otherValue but not the actual values.

What am I missing here?

1 Answer 1

1

Object.keys() returns an array of the keys in the object, which explains why only the keys are being printed out. Try using Object.values() instead (ES2017):

Object.values(data.objectOne).map(value => <div key={value}>{value}</div>

Alternatively, you can stick with Object.keys and then use bracket notation to get the value for that key from the data.objectOne object:

Object.keys(data.objectOne).map(field => <div key={field}>{data.objectOne[field]}</div>
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.