get(key|index, default?)

Gets the value for a key in a map.

If there is no entry with the request key, and the user calls get with a second argument even if that argument is undefined, the second argument will be returned instead. Otherwise, get will return the result of getDefault(key), which itself defaults to returning undefined.

Every map implementation gives an opportunity to override getDefault through the constructor, but it can always be overridden on either the instance or the prototype. It is often useful to provide a getDefault that will create, save, and return a default instance for a given key.

var users = new Dict();
users.getDefault = function (id) {
    var user = new User(id);
    this.set(id, user);
    return user;
};

For the purposes of the get and set methods, an Array behaves like a map from index to the value at that index.

var value = [1, 2, 3].get(1);
expect(value).toBe(2);

In contrast, for the purposes of the has method, an Array behaves as a list of values.

var found = [1, 2, 3].has(1);
expect(found).toBe(true);

On collections

Usage

  • get(key)
  • get(key, default)
  • get(index)
  • get(index, default)

Related