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()

Extends

  • Map

Members

contents :Array.<*>

Return an Array of all the entry values in the Collection

Type:

Methods

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

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.<*>

find(condition) → {*}

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 undefined

Type
*

forEach(fn)

See:
  • Array#forEach

Apply a function to each element of the collection

Example
let c = new Collection([["a", {active: false}], ["b", {active: false}], ["c", {active: false}]]);
c.forEach(e => e.active = true);
Parameters:
Name Type Description
fn function

The function to apply

get(key, optionsopt) → {*|undefined}

Get an element from the Collection by its key.

Example
let c = new Collection([["a", "Alfred"], ["b", "Bob"], ["c", "Cynthia"]]);
c.get("a"); // "Alfred"
c.get("d"); // undefined
c.get("d", {strict: true}); // throws Error
Parameters:
Name Type Attributes Description
key string

The key of the entry to retrieve

options object <optional>

Additional options that affect how entries are retrieved

Properties
Name Type Attributes Default Description
strict boolean <optional>
false

Throw an Error if the requested key does not exist. Default false.

Returns:

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

Type
* | undefined

getName(name, optionsopt) → {*}

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

Example
let c = new Collection([["a", "Alfred"], ["b", "Bob"], ["c", "Cynthia"]]);
c.getName("Alfred"); // "Alfred"
c.getName("D"); // undefined
c.getName("D", {strict: true}); // throws Error
Parameters:
Name Type Attributes Description
name string

The name of the entry to retrieve

options object <optional>

Additional options that affect how entries are retrieved

Properties
Name Type Attributes Default Description
strict boolean <optional>
false

Throw an Error if the requested name does not exist. Default false.

Returns:

The retrieved entry value, if one was found, otherwise undefined

Type
*

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

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.<*>

reduce(evaluator, initial) → {any}

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

some(condition) → {boolean}

See:
  • {Array#some}

Test whether a condition is met by some entry in the Collection

Parameters:
Name Type Description
condition function

A test condition to apply to each entry

Returns:

Was the test condition passed by at least one entry?

Type
boolean

toJSON() → {Array.<object>}

Convert the Collection to a primitive array of its contents.

Returns:

An array of contained values

Type
Array.<object>