Jasper
Jasper

Reputation: 2231

React: getting value from state object by index

I am struggling with the following problem. I've written a react component that inherits the following objects as props

assessment: {questions: {question1: 'answer1', 'question2': 'answer2'}

block_index: 1 (could be any number).

I want to pass the right question to the 'Quizbuilder' object by using the 'block_index' to fetch the right question.

But for reasons I do not understand

this.state.assessment.questions[1] 

returns a value

while

this.state.assessment.questions[this.state.block_index] 

does not.

They are passed to the 'Quizbuild component' like so

<QuizBuilder quiz_object={this.state.assessment.questions[thi.state.block_index]} />

I must be missing something, but for the life of me I cannot figure out whats wrong with my code. Does anyone have an idea?

Upvotes: 0

Views: 3119

Answers (1)

Mos&#232; Raguzzini
Mos&#232; Raguzzini

Reputation: 15821

Try this:

const assessment =  {questions: {question1: 'answer1', 'question2': 'answer2'}};

console.log(Object.keys(assessment.questions)[0])

Upvotes: 1

Related Questions