0

I'm trying to get a value from an object and I just can't get it. The maximum I get is that,have using console.log(randomWords)

I'm sure I'm doing something wrong here, but I already spent some long hours trying to find the error. The object looks fine and I just can't find a way to access these deep values.

  var words = {
      "that": {
         "languages": {
            "pt": {
               "multiple_meanings": true,
               "meaning": ["aquilo", "aquela"]
            },
            "it": {
               "multiple_meanings": false,
               "meaning": "quella"
            }
         }
      },
      "this": {
         "languages": {
            "pt": {
                "multiple_meanings": true,
                "meaning": ["este", "esta"]
            },
            "it": {
                "multiple_meanings": true,
                "meaning": ["questo", "questa"]
            },
          }
        }
    };
    var userChoseLanguage = "pt"
    var wordsKeysArr = Object.keys(words).toArray; 
    var wordIndex = Math.floor(Math.random() * wordsKeysArr.length);

    //store a random word in randomWord
     var randomWord = recipesKeysArr[factIndex];

    //if user chose pt
     if(userChoseLanguage == "pt"){
        //if multiple_meanings = true
        if(randomWord.languages.multiple_meanings===true) {
           console.log("This word has more than one meaning");
        }
        else {
           console.log(randomWord.languages.meaning);
        }
     }
Saravanan Sachi
  • 2,572
  • 5
  • 33
  • 42
spaceman
  • 659
  • 2
  • 11
  • 29
  • What you have is not JSON. It's JavaScript. There is nothing related to JSON in your example. – Felix Kling Dec 19 '16 at 07:58
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Dec 19 '16 at 07:59

1 Answers1

0

"recipesKeysArr[factIndex]" returns only the value based on the index not the whole object.

So you can use it as index to access that particular object in "words".

if (userChoseLanguage == "pt") {
    //if multiple_meanings = true
    if (words[randomWord].languages[userChoseLanguage].multiple_meanings === true) {
        console.log("This word has more than one meaning");
    }     
    else {
        console.log(words[randomWord].languages[userChoseLanguage].meaning);
    }
}
Saravanan Sachi
  • 2,572
  • 5
  • 33
  • 42