Is there a dictionary implementation in JavaScript?
Technically not, but you can use a regular JavaScript object like a dictionary:
var a = {"a":"wohoo", 2:"hello2", "d":"hello"};alert(a["a"]);alert(a[2]);alert(a["d"]);
John Resig (author of jQuery) posted recently on dictionary lookups in javascript.
His solution is to assign the dictionary values as properties of an object. Code pasted verbatim from above article:
// The dictionary lookup objectvar dict = {};// Do a jQuery Ajax request for the text dictionary$.get( "dict/dict.txt", function( txt ) { // Get an array of all the words var words = txt.split( "\n" ); // And add them as properties to the dictionary lookup // This will allow for fast lookups later for ( var i = 0; i < words.length; i++ ) { dict[ words[i] ] = true; } // The game would start after the dictionary was loaded // startGame();});// Takes in an array of letters and finds the longest// possible word at the front of the lettersfunction findWord( letters ) { // Clone the array for manipulation var curLetters = letters.slice( 0 ), word = ""; // Make sure the word is at least 3 letters long while ( curLetters.length > 2 ) { // Get a word out of the existing letters word = curLetters.join(""); // And see if it's in the dictionary if ( dict[ word ] ) { // If it is, return that word return word; } // Otherwise remove another letter from the end curLetters.pop(); }}
You can try buckets, is a javascript data structure library, it allows you to use any type of object in a dictionary.