0

I'm not sure how to explain correctly in a title, but here is the situation:

Having three available angles: front, left and right:

captures: {
  front: { img: '', correct: false },
  left: { img: '', correct: false },
  right: { img: '', correct: false }
},

I have defined a default variable named this.currentAngle = 'front'.

I want to access a value from CAPTURES object list within currentAngle.

This does not work:

this.captures.[this.currentAngle].img

nor this one, logically

this.captures.this.currentAngle.img

What's the correct approach ?

aspirinemaga
  • 3,753
  • 10
  • 52
  • 95

1 Answers1

1

You almost got it, just remove the dot before the opening square bracket from your first attempt:

const captures = {
  front: { img: 'f', correct: false },
  left: { img: 'l', correct: false },
  right: { img: 'r', correct: false }
}

let prop = 'front'

console.log(captures[prop].img)
connexo
  • 53,704
  • 14
  • 91
  • 128