getValue

Category:Variables

Gets the value associated with a specific key in an object.

An object is a way to collect data and assign a unique label to each item. One example of a real-life object is a dictionary - every word has a corresponding definition. Here's how a dictionary would look as an App Lab object:

var dictionary = {
    "tortilla": "a thin, flat pancake of cornmeal or flour, eaten hot or cold, typically with a savory filling as a part of Mexican cuisine.",
    "naan": "a type of leavened bread, typically of teardrop shape and traditionally cooked in a clay oven as part of Indian cuisine.",
    "injera": "a white leavened Ethiopian bread made from teff flour, similar to a crêpe."
}

In the example above, each word in the dictionary is the key - it's what you use to look up values in the dictionary. The definitions are the values - they are what are trying to look up in the dictionary.

You can use getValue to look up a value from its key. This would be like looking up the definition of a word in the dictionary.

Examples

Dictionary Lookup

Use getValue() to look up the definition of words in a dictionary object

var dictionary = {
    "tortilla": "a thin, flat pancake of cornmeal or flour, eaten hot or cold, typically with a savory filling as a part of Mexican cuisine.",
    "naan": "a type of leavened bread, typically of teardrop shape and traditionally cooked in a clay oven as part of Indian cuisine.",
    "injera": "a white leavened Ethiopian bread made from teff flour, similar to a crêpe."
}

var definition = getValue(dictionary, "tortilla");
console.log("Tortilla: " + definition);
definition = getValue(dictionary, "naan");
console.log("Naan: " + definition);

Phone Number Lookup

Use a dictionary to look up someone's phone number

var phoneNumbers = {
	"Brendan": "520-555-1827",
    "Alicia": "510-555-9182",
    "Omar": "720-555-2817",
    "Emergency": "911"
};

var name = prompt("Enter the phone number you want to look up (Brendan, Alicia, Omar, Emergency)");
var number = getValue(phoneNumbers, name);
console.log(name + ": " + number);

Abbreviation Dictionary

Teach a chatbot what new abbreviations mean

var abbreviations = {
	"LOL": "Laugh out loud",
    "TIL": "Today I Learned",
    "LGTM": "Looks Good To Me",
    "FWIW": "For What It's Worth",
    "FYI": "For Your Information"
};

console.log("Hello! I am learning new abbreviations! Would you like to look up an abbreviation, or teach me a new one?);
var choice = prompt("(1) Look Up an Abbreviation (2) Teach a New Abbreviation);

if(choice == 1) {
	var word = prompt("What abbreviation do you want to look up?");
    var definition = getValue(abbreviations, word);
    console.log(word + ": " + definition);
}
if(choice == 2) {
	var word = prompt("What is the new abbreviation?");
    var definition = prompt("What does it mean?");
    addPair(abbreviations, word, definition);
    console.log("Successfully Added!");
}

Syntax

getValue(object, key)

Parameters

NameTypeRequired?Description
object Object The object to access the value from.
key String The name of the key to look up the value for in `object`.

Returns

The value stored in object under the label key.

Found a bug in the documentation? Let us know at documentation@code.org