0
demoDiction = {
    is: "es",
    the: 'la',
    best: 'mejor'
}

function trnsToSpan (word){
    alert(demoDiction.word)
}

Above is a small fragment of code. It has a function to translate a word to another language. How can I refer the same property of demoDiction object matching to the value of "word" variable. In other words if "word" variable has value "is" then it should alert the "es" and in the same way for "the", "best" words.

Bas
  • 469
  • 8
  • 22

1 Answers1

1

Change your function to:

function trnsToSpan (word){
    alert(demoDiction[word]);
}

That uses bracket notation to access the property of demoDiction

Amit Joki
  • 58,320
  • 7
  • 77
  • 95