JavaScript dictionary help?

Hi,

I’m trying to create a prompt that provides a selected value that I can then use to retrieve a value from a key/value dictionary. In other words, in the code below, if a user chooses “Inbox” from the prompt, I want var b to now have the UUID value of dict.inbox

I don’t really understand JavaScript, I’m just cutting and pasting from other folks examples. Maybe this is the wrong way to go about this? At any rate, the code below isn’t working.

var locs = ['Inbox','Readings','Teaching','ReadingNotes'];

var t = Prompt.create();
t.title = "Save where?";

t.addSelect("loc","locations", locs,[""],false);

t.addButton("OK");

var didSelect = t.show();

// location dictionary
var dict = new Object();

dict.Inbox = "9BE1F258-EC8E-4A04-8331-6ED5D1A30D2E";
dict.Readings = "FC55AA1F-F6BA-4216-A589-302693ED8505";
dict.Teaching = "F396C26F-B4DA-47EF-B777-CE612805EFC0";
dict.ReadingNotes = "9D3A6BBD-0F68-4DB4-9A3A-B9EDF2F9B5D8";

//get value from prompt and translate to devonthink location uuid
var a = t.fieldValues["loc"];
var b = dict.a;

Try changing the last line to this:

var b = dict[a];

That did it. Thanks!!

Is there a name for what’s going on with those brackets? I’m assuming it’s not something to do with an array … just curious to look it up and learn more.

I think I’ve seen it called simply “bracket notation”. Effectively it is just accessing an object’s property by name such that the name can be dynamically specified (i.e. a variable) rather than using “dot notation”, where the property name is fixed as part of the code (i.e not a variable, constant, etc.) There are quite a few people here who have a deeper knoweldge of JavaScript than I do who may(?) be able to give you a more definitive name.

that term leads to several useful guides to bracket vs dot notation and manipulation of variables. Thanks!