Reputation: 12716
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];
}
}
Upvotes: 0
Views: 156
Reputation: 4228
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/
Upvotes: 1
Reputation: 198
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];
}
}
}
Upvotes: 1