ShakaCode | ShakaCode Blog | Rails On Maui Blog | Rails | ReactJs | JavaScript | Webpack | Productivity |

Reactjs component map array of objects


#1

Need to map array of objects. Keep getting “Cannot read property ‘map’ of undefined”.

survey.questions.map((question)=>{
console.log('the question: ',question);
})

survey json looks something like this…

[{

	"questions": [{
				"name": 0,
				
			}, {
				"name": 1,
	
			},
			{
				"name": 3,
	
			},
	}]

}]


#2

survey.questions is null.

put in a print statement and you’ll see that.


#3

When the state renders before the api call is made survey.questions might be an empty object. you can do this instead {survey.questions && survey.questions.map((question)=> {
console.log('thequestion: ', question);
})

also you can debug with console.log