Tags : JavaScript Intro



Hashes

Objects(Hashes) are a structured way to organize data. Remember they don't have an index position like arrays.

console

// Hashes are define like this

{}

// this is an empty hash

var hashExample = {key1: "one", key2: "two"};

// we assigned a hash to hashExample with 2 key value pairs.

// we access the values of each key like this

hashExample.key1

hashExample.key2

// Let's assign an array inside a value of the first key of the hash.

var hashExample2 = {key1: ["one","two", "three"], key2: "nothing interesting here"};

hashExample2.key1

-> ["one","two", "three"]

// Now we can use the index to get an element of the array stored in the hash.

hashExample2.key1[0]

-> "one"

// this works because we are actually applying the index to value which is an array.

Try the following:

  • Try to build your own hash with multiple key/value pairs.
  • Add an array to one of the values and use the index to get a value of the array.