0

I have an object of objects containing arrays. I'd like to loop through them. For the subjects object within vocab, parseHoveredText()'s log("log: " + obj + ", " + vocab[obj][word.toLowerCase()][0]); works fine.

Output: log subjects, You

But for the other objects, such as nouns, verbs, etc... , log("log: " + obj + ", " + vocab[obj][word.toLowerCase()][0]); gives:

Output: Cannot read property '0' of undefined

I don't see how that is possible. If I put 'má' : ['Hemp', path+n+'Ma2_Hemp.mp3'], within the subjects object, it works fine... so I think there is something wrong with the loop.

So if I change the log to log("log: " + obj + ", " + vocab[obj][word.toLowerCase()]);:

Output:

log: subjects, undefined 
log: nouns, Mother,recordings/nouns/Ma1_Mother.mp3 
log: verbs, undefined 
log: measure, undefined 
log: adjectives, undefined 
log: adverbs, undefined 
log: prepositions, undefined 
log: particles, undefined
log: suffix, undefined 

So it's finding the keys from the other objects, as you can see it returned Mother from Nouns.

Code:

var path = 'recordings/';
var sbj = 'subjects/';
var n = 'nouns/';

var vocab = 
{
    "subjects" :
    {   
        'wǒ'    : ['I/Me',              path+sbj+'Wo_I.mp3'],
        'nǐ'    : ['You',               path+sbj+'Ni_You.mp3'],
        'tā'    : ['Him/Her',           path+sbj+'Ta_him.mp3'],
        'shuí'  : ['Who',               path+sbj+'Shui_Who.mp3']
    },
    "nouns" : 
    {
        'xièxiè': ['Thanks',            path+n+'Xiexie4_Thanks.mp3'],
        'duì'   : ['Correct/at/facing', path+n+'Dui4_Facing.mp3'],
        'má'    : ['Hemp',              path+n+'Ma2_Hemp.mp3'],
    etc...

function parseHoveredText (word, audio) {
    for (obj in vocab) {
    log("log: " + obj + ", " + vocab[obj][word.toLowerCase()][0]);
        if(audio) {
            return vocab[obj][word.toLowerCase()][1];
        }
        return vocab[obj][word.toLowerCase()][0];
    }
}
4
  • Have you extended Object.prototype? Commented Feb 8, 2014 at 22:44
  • @cookiemonster what does that do and how would I extend it? Commented Feb 8, 2014 at 22:45
  • Makes every object inherit that property. It can cause problems in various ways, like when enumerating the properties of an object. What are the arguments you've given to parseHoveredText()? Commented Feb 8, 2014 at 22:46
  • ...also, does every property of vocab have an object that contains the word property you're passing? If not, vocab[obj][word.toLowerCase()] would be undefined, and so you'd not be able to access property [0]. Commented Feb 8, 2014 at 22:50

2 Answers 2

1

Looks like there's something a little wrong with your logic there. What's happening is that you pass a word, I'm assuming such as 'wǒ', into your parseHoveredText() function and that function tries to reference the word in every vocab object. This will always cause an error for noun's or anything that is not a subject, because it will try to find the word in the subject array first.

You should be able to fix this by changing your function to:

function parseHoveredText (word, audio) {
    for (obj in vocab) {
        if(vocab[obj][word.toLowerCase()]) {
            log("log: " + obj + ", " + vocab[obj][word.toLowerCase()][0]);
            if(audio) {
                return vocab[obj][word.toLowerCase()][1];
            }
            return vocab[obj][word.toLowerCase()][0];
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This fixed it. I knew there was something wrong with the loop, but couldn't figure out what happened. Thanks so much!
1

The problem is you are looping through each element of the vocab object starting with "subjects" if it can't find the "word", it can't execute command log because the variable is not defined. in javascript if a line of code breaks, the execution will stop. therefore you won't see the log you'd see otherwise. try checking if that is defined first:

function parseHoveredText (word, audio) {
    for (obj in vocab) {
        if(typeof vocab[obj][word.toLowerCase()] !== "undefined")
        {
    console.log("log: " + obj + ", " + vocab[obj][word.toLowerCase()][0]);
        if(audio) {
            return vocab[obj][word.toLowerCase()][1];
        }
        return vocab[obj][word.toLowerCase()][0];
        }
    }
}

jsfiddle: http://jsfiddle.net/A83pF/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.