0

I am wondering if it is possible to render an object's keys in React Native using the map function. I am retrieving data from an api and one of the objects in the array returns the key and value. I am looking to render the keys of each object (each object can have 1-3 keys/values).

I've tried using the following but find out that mapping the object doesn't work because it is not a function of the object.

  {apiData.objectData.map((objectKey) => (
    <Text>{objectKey}</Text>
  ))}

enter image description here

4
  • Show us the example of your "apiData" Commented Oct 12, 2021 at 14:12
  • Updated with image of the objectData. Commented Oct 12, 2021 at 14:14
  • and what is your expectation from the above code snippet? Commented Oct 12, 2021 at 14:15
  • I am expecting it to return each key for example in the image it would display: "unlimited" in text. Commented Oct 12, 2021 at 14:16

1 Answer 1

1

You can use #Object.keys():

<View>
  {Object.keys(apiData.objectData).map((objectKey) => (
    <Text key={objectKey}>{objectKey}</Text>
  ))}
</View>

// Example from docs
const obj = { 0: 'a', 1: 'b', 2: 'c' };
console.log(Object.keys(obj)); // console: ['0', '1', '2']
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.