Collection

Collection

A reusable storage concept which blends the functionality of an Array with the efficient key-based lookup of a Map. This concept is reused throughout Foundry VTT where a collection of uniquely identified elements is required.

Constructor

new Collection()

Source:

Extends

  • Map

Members

entries

Source:

Return an Array of all the entry values in the Collection

Methods

filter(condition) → {Array.<V>}

Source:
See:
  • {Array#filter}

Filter the Collection, returning an Array of entries which match a functional condition.

Example
let c = new Collection([["a", "AA"], ["b", "AB"], ["c", "CC"]]);
let hasA = c.filters(entry => entry.slice(0) === "A");
Parameters:
Name Type Description
condition function

The functional condition to test

Returns:

An Array of matched values

Type
Array.<V>

find(condition) → {V|null}

Source:
See:
  • {Array#find}

Find an entry in the Map using an functional condition.

Example
let c = new Collection([["a", "A"], ["b", "B"], ["c", "C"]]);
let a = c.find(entry => entry === "A");
Parameters:
Name Type Description
condition function

The functional condition to test

Returns:

The value, if found, otherwise null

Type
V | null

get(key, strict) → {V|null}

Source:

Get an element from the Collection by its key.

Example
let c = new Collection([["a", "A"], ["b", "B"], ["c", "C"]]);
c.get("a"); // "A"
c.get("d"); // null
c.get("d", {strict: true}); // throws Error
Parameters:
Name Type Description
key string

The key of the entry to retrieve

strict boolean

Throw an Error if the requested id does not exist, otherwise return null. Default false

Returns:

The retrieved entry value, if the key exists, otherwise null

Type
V | null

getName(name, strict) → {Entity|null}

Source:

Get an entry from the Collection by name. Use of this method assumes that the objects stored in the collection have a "name" attribute.

Parameters:
Name Type Description
name string

The name of the entry to retrieve

strict boolean

Throw an Error if the requested id does not exist, otherwise return null. Default false.

Returns:

The retrieved Entity, if one was found, otherwise null;

Type
Entity | null

map(transformer) → {Array.<V>}

Source:

Transform each element of the Collection into a new form, returning an Array of transformed values

Parameters:
Name Type Description
transformer function

The transformation function to apply to each entry value

Returns:

An Array of transformed values

Type
Array.<V>

reduce(evaluator, initial) → {any}

Source:
See:
  • {Array#reduce}

Reduce the Collection by applying an evaluator function and accumulating entries

Example
let c = new Collection([["a", "A"], ["b", "B"], ["c", "C"]]);
let letters = c.reduce((s, l) => {
  return s + l;
}, ""); // "ABC"
Parameters:
Name Type Description
evaluator function

A function which mutates the accumulator each iteration

initial any

An initial value which accumulates with each iteration

Returns:

The accumulated result

Type
any