Efficient syntax for populating a javascript associative array
问题 I have an autocomplete text box that users can type an item code into and need to find out what the id number of that item code is in javascript. An associative array is the way I would imagine it should be done, but the following seems a little long winded and I'm hoping someone has a better way to do it or shorthand of what I have below: var itemIds = new Array(); itemIds["item1"] = 15; itemIds["item2"] = 40; itemIds["item3"] = 72; ... function getItemId(code){ return itemIds[code]; } 回答1: