2

I have an object that contains all the months:

const months = {
        "january": 31,
        etc...

Then i have a text input. I select the value from the input with javascript (.value) in a variabel called "value".

When I type "january" (without quote marks) in the text input and try doing console.log(months.value); nothing happens.

How can I convert the value a datatype (or something else) that can be used to select "january" in the object "months"?

Reyni
  • 69
  • 7

2 Answers2

1

A string is fine to select an item from an object, but you should use square bracket notation when you want to get your key name from a variable:

console.log(months[value])
0

As far as I understand you need this months[“january”] Or months[value]

Alexander Vitanov
  • 4,074
  • 2
  • 19
  • 22