[React]How to transform an object into an array inside a props[closed]

Hi @Yumenotsuki (FYI, if You want people to know you are referencing them so they get a notification, you need to type the “@” symbol, and a list of forum members with the participants in that thread will pop up). Glad to see you’re making progress. You are correct that the error you’re getting is because _this2.props.test.reponses[questionIndex] doesn’t have a map function, i.e.: it isn’t an array. The real question I have is: is this ALL the code for your class component? Because right now, questionIndex is initialized to an empty array in the constructor.

In JS, all Arrays are subclasses of Objects, so instead of any string being a legal key, the keys in Arrays are all numbers. I believe what your code is trying to do is to coerce the array questionIndex ([]) into a string, looking up that key in responses, and failing to find any value. Thus, reponses[questionIndex].map() is the same as undefined.map().

I wrote a quick fiddle to show you what happens when you access empty members of an array or undefined properties on an object. Notice that when logging or printing the array, JS says an undefined “property/element” on an Array is an “empty slot”, not undefined, but when accessing that empty slot, the Array’s getter function returns undefined (which you can see with the console.log statements):
https://jsfiddle.net/vipatron/2pgeb3mx/2/

As I said above, we can’t see the code which you use to parse the JSON you are getting back from the API call from your first thread. That’s why we can’t help you better. Based on my understanding from that other thread, the JSON returns everything you need in order to display 1 question: all the questionElements combine to make 1 fill-in-the-blank question, and all the reponses are answer choices that are possible answers for that single question, and a reponseCorrecte which allows you to decide whether the user chose correctly. So, unless you are prefetching all the questions needed for the whole quiz, I can’t tell why you even need a questionIndex variable. Even if you are prefetching all the questions, wouldn’t questionIndex just be a Number, not an Array? If you can show us the code you are using to turn the JSON into state, that’d be very helpful.