Using Objects for Lookups code example

Example 1: Using Objects for Lookups

function phoneticLookup(val) {
  var result = "";

  var lookup = {
     "alpha":"Adams",
     "bravo":"Boston",
     "charlie":"Chicago",
     "delta":"Denver",
     "echo":"Easy",
     "foxtrot":"Frank",
  };
  result = lookup[val];
  return result;
}

phoneticLookup("charlie");
console.log(phoneticLookup("alpha")); // to test
console.log(phoneticLookup("")); //to test

Example 2: basic javascript using objects for lookups

var alpha = {
  1:"Z",
  2:"Y",
  3:"X",
  4:"W",
  24:"C",
  25:"B",
  26:"A"
};
alpha[2]; // "Y"
alpha[24]; // "C"

var value = 2;
alpha[value]; // "Y"

Example 3: using objects for lookups in javascript

var alpha = {
  1:"Z",
  2:"Y",
  3:"X",
  4:"W",
  24:"C",
  25:"B",
  26:"A"
};
alpha[2]; // "Y"
alpha[24]; // "C"

var value = 2;
alpha[value]; // "Y"

Tags:

Misc Example