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];
}
}
Object.prototype?parseHoveredText()?vocabhave an object that contains thewordproperty you're passing? If not,vocab[obj][word.toLowerCase()]would beundefined, and so you'd not be able to access property[0].