How to declare nested objects in JavaScript? -
i'm trying create object contains object, think of dictionary:
var dictionaries = {}; dictionaries.english_to_french = { {english:"hello",french:"bonjour"}, {english:"i want",french:"je veux"}, {english:"bla",french:"le bla"} };
but gives error uncaught syntaxerror: unexpected token {
doing wrong?
thanks !
edit
i'm sorry did not clarify want do. edited code above.
you're trying give object property, , property single object:
dictionaries.english_to_french = {english:"hello",french:"bonjour"} ;
you don't need { }
. declare whole thing @ once:
var dictionaries = { english_to_french: { english: "hello", french: "bonjour" } };
i suggest better format dictionaries might be:
var dictionaries = { english_to_french: { "hello": "bonjour", "chicken": "poulet", // ? "englishman": "rosbif" } };
that way can words directly without having search. create reverse dictionary that:
dictionaries.french_to_english = function(dict) { var rv = {}; (var eword in dict) rv[dict[eword]] = eword; return rv; }(dictionaries.english_to_french);
Comments
Post a Comment