API Docs for: 3.11.0-git
Show:

Map Class

An ordered hash map data structure with an interface and behavior similar to (but not exactly the same as) ECMAScript 6 Maps.

Constructor

Map

(
  • [entries]
  • [options]
)

Parameters:

  • [entries] Array[] | Map optional

    Array or Map of entries to add to this map. If an array, then each entry should itself be an array in which the first item is the key and the second item is the value for that entry.

  • [options] Object optional

    Options.

    • [autoStamp=false] Boolean optional

      If true, objects used as keys will be automatically stamped with a unique id as the value of the property defined by the objectIdName option ("_yuid" by default) if that property isn't already set. This will result in much faster lookups for object keys.

    • [objectIdName="_yuid"] String optional

      Name of a property whose string value should be used as the unique key when present on an object that's given as a key. This will significantly speed up lookups of object-based keys that define this property.

Methods

_indexMapKey

(
  • index
  • key
)
protected

Indexes the given key to enable faster lookups.

Parameters:

  • index Number

    Numerical index of the key in the internal _mapKeys array.

  • key Mixed

    Key to index.

_indexOfKey

(
  • key
)
Number protected

Returns the numerical index of the entry with the given key, or -1 if not found.

This is a very efficient operation with string keys, but may be slower with non-string keys.

Parameters:

  • key Mixed

    Key to look up.

Returns:

Number:

Index of the entry with the given key, or -1 if not found.

_isSafeKey

(
  • key
)
Boolean protected

Returns true if the given string key is safe to use in environments that don't support Object.create().

Parameters:

Returns:

Boolean:

true if the key is safe.

_reindexMap

() protected

Reindexes all the keys in this map.

_removeMapEntry

(
  • index
)
protected

Removes the entry at the given index from internal arrays.

This method does not update the size property.

Parameters:

  • index Number

    Index of the entry to remove.

_sameValueZero

(
  • a
  • b
)
Boolean protected

Returns true if the two given values are the same value, false otherwise.

This is an implementation of the ES6 SameValueZero comparison algorithm. It's more correct than === in that it considers NaN to be the same as NaN.

Note that 0 and -0 are considered the same by this algorithm.

Parameters:

  • a Mixed

    First value to compare.

  • b Mixed

    Second value to compare.

Returns:

Boolean:

true if a and b are the same value, false otherwise.

_updateMapSize

() protected

Updates the value of the size property in old browsers. In ES5 browsers this is a noop, since the size property is a getter.

clear

() chainable

Deletes all entries from this map.

concat

(
  • [maps*]
)
Map

Returns a new map comprised of this map's entries combined with those of the maps or arrays passed as arguments. Does not alter this map or those given as arguments in any way.

Entries in later (rightmost) arguments will take precedence over entries in earlier (leftmost) arguments if their keys are the same.

This method also accepts arrays of entries in lieu of actual Y.Map instances.

The returned map will be created using the same options and constructor as this map.

Parameters:

  • [maps*] Array[] | Map optional

    Zero or more maps or entry arrays to concatenate into the resulting map.

Returns:

Map:

New map containing the concatenated values of this map and all arguments.

delete

()

Alias for remove().

each

(
  • callback
  • [thisObj]
)
chainable

Executes the given callback function on each entry in this map.

To halt iteration early, return false from the callback.

Parameters:

  • callback Function

    Callback function.

    • value Mixed

      Value being iterated.

    • key Mixed

      Key being iterated.

    • map Map

      Reference to this map.

  • [thisObj] Object optional

    this object to use when calling callback.

entries

() Array

Returns an array of all the entries in this map. Each entry is an array with two items, the first being a key and the second a value associated with that key.

Returns:

Array:

Array of entries.

forEach

()

Alias for each().

get

(
  • key
  • [defaultValue]
)
Mixed

Returns the value associated with the given key, or default if the key isn't found.

Parameters:

  • key Mixed

    Key to look up.

  • [defaultValue] Mixed optional

    Default value to return if key isn't found.

Returns:

Mixed:

Value associated with the given key, or default if the key isn't found.

has

(
  • key
)
Boolean

Returns true if key exists in this map, false otherwise.

Parameters:

  • key Mixed

    Key to look up.

Returns:

Boolean:

true if key exists in this map, false otherwise.

keys

() Array

Returns an array of all the keys in this map.

Returns:

Array:

Array of keys.

merge

(
  • maps
)
chainable

Merges the entries from one or more other maps or entry arrays into this map. Entries in later (rightmost) arguments will take precedence over entries in earlier (leftmost) arguments if their keys are the same.

This method also accepts arrays of entries in lieu of actual Y.Map instances, so the following operations have the same result:

// This...
map.merge(new Y.Map([['a', 'apple'], ['b', 'bear']]));

// ...has the same result as this...
map.merge([['a', 'apple'], ['b', 'bear']]);

Parameters:

  • maps Array[] | Map multiple

    One or more maps or entry arrays to merge into this map.

remove

(
  • key
)
Boolean

Deletes the entry with the given key.

Parameters:

  • key Mixed

    Key to delete.

Returns:

Boolean:

true if the key existed and was deleted, false otherwise.

set

(
  • key
  • value
)
chainable

Sets the value of the entry with the given key. If the key already exists, its value will be overwritten; otherwise it will be created.

The key may be any JavaScript value (including both primitives and objects), but string keys will allow fast lookups, whereas non-string keys may result in slower lookups.

Parameters:

  • key Mixed

    Key to set.

  • value Mixed

    Value to set.

toJSON

() Array

Returns a JSON-serializable representation of this map.

This is effectively an alias for entries(), but could be overridden to return a customized representation.

Returns:

Array:

JSON-serializable array of entries in this map.

values

() Array

Returns an array of all the values in this map.

Returns:

Array:

Array of values.

Properties

_isIndexStale

Boolean protected

Whether or not the internal key index is in need of reindexing.

Rather than reindexing immediately whenever it becomes necessary, we use this flag to allow on-demand indexing the first time an up-to-date index is actually needed.

This makes multiple remove() operations significantly faster, at the expense of a single reindex operation the next time a key is looked up.

_mapKeyIndices

Object protected

Internal index mapping string keys to their indices in the _mapKeys array.

_mapKeys

Array protected

Internal array of the keys in this map.

_mapObjectIndices

Object protected

Internal index mapping object key ids to their indices in the _mapKeys array. This is separate from _mapKeyIndices in order to prevent collisions between object key ids and string keys.

_mapOptions

Object protected

Options that affect the functionality of this map.

_mapValues

Array protected

Internal array of the values in this map.

size

Number

The number of entries in this map.

Default: 0