1) I have this Javascript array:
lang=new Array();
lang["sq"]="Albanian";
lang["ar"]="Arabic";
lang["en"]="English";
lang["ro"]="Romanian";
lang["ru"]="Russian";
2) In some other process, there is a returned value in a variable:
result.detectedSourceLanguage = 'en';
3) Now, i want to print the language full name by doing this:
alert(lang[result.detectedSourceLanguage]);
The dialog displays: undefined
Any ideas?
BTW: im using JQuery, so JQuery solutions are welcomed.
-
Did you try using index's? 0,1,2,3? When declaring variable did you use VAR? Try also calling that variable "resultDetectedSourceLanguage"... Dont know what else to say... :)Gavrisimo– Gavrisimo2009年09月06日 02:47:05 +00:00Commented Sep 6, 2009 at 2:47
-
May be check that you have the array defined by the time you are trying to access it. BTW What do you mean by "in some other process"?Jaime– Jaime2009年09月06日 02:49:56 +00:00Commented Sep 6, 2009 at 2:49
-
if i call: alert(lang['en']); it prints English as it should. The problem is when i do this: alert(lang[var1]); (where var1 holds 'en' value).Andres SK– Andres SK2009年09月06日 02:55:29 +00:00Commented Sep 6, 2009 at 2:55
-
Well that assertion is impossible; regardless of your misuse of Array (should be Object, {}), JavaScript does allow this, and your code above does work as expected. There is something else happening; post a full test case that fails.bobince– bobince2009年09月06日 23:40:59 +00:00Commented Sep 6, 2009 at 23:40
4 Answers 4
An Array uses integer indexes. You probably want an Object, which supports string indexes:
lang=new Object();
lang["sq"]="Albanian";
lang["ar"]="Arabic";
lang["en"]="English";
lang["ro"]="Romanian";
lang["ru"]="Russian";
// or
lang = {
'sq': 'Albanian',
'ar': 'Arabic',
// ...
'ru': 'Russian'
};
(The latter example is probably better as more JS programmers would be happy with it.)
2 Comments
['val 1', 'val 2', 'val 3, ...] syntax. Its likely that somewhere in JQuery or elsewhere a serialization/deserialization is happening to the array, and the additional properties are lost.Array is an Object, but really, you should treat it as a sequential array (like std::vector in C++'s STL) and not an associative array (like std::map in C++'s STL or PHP-like arrays), to avoid confusion. Serialization/conversion may be the problem as you suggest, Adam.This script generates a message box (checked in IE & FF) that says "English":
lang = new Array();
lang["sq"] = "Albanian";
lang["ar"] = "Arabic";
lang["en"] = "English";
lang["ro"] = "Romanian";
lang["ru"] = "Russian";
detectedSourceLanguage = 'en';
alert(lang[detectedSourceLanguage]);
The only problem could be the result object.
1 Comment
Check the type and value of result (and result.detectedSourceLanguage). It could be one of the following
resultis not definedresultis not an object or doesn't have any attribute nameddetectedSourceLanguage- Value of
result.detectedSourceLanguageis not a string or there's no such key inlang(then it's supposed to returnundefinedforalert(lang[result.detectedSourceLanguage]);)
BTW, your problem has nothing to do with jQuery
1 Comment
Try alerting result.detectedSourceLanguage immediately prior to its use. There is a chance that it doesn't equal what you expect it to. This should work.