Bag

A simple collection class to contain unique, keyed items.

Properties

2
count: Numberreadonly

The number of items in this Bag.

values: Object[]

The set of values of this Bag.

Setting this property replaces the data set.

Functions

12

Adds the passed item(s) to this Bag. Existing items with the same ID will be replaced.

ParameterTypeDescription
toAddObject | Object[]

Item(s) to add.

Returns nth item in this Bag.

ParameterTypeDescription
nthNumber

The index of the matching item. Negative numbers index for the last item. For example, -1 returns the last item, -2 the 2nd to last item etc.

Returns: Object -

The matched item, or undefined.

Change the id of an existing member by mutating its idProperty.

ParameterTypeDescription
itemString | Number | Object

The item or id of the item to change.

newIdString | Number

The id to set in the existing member.

Returns the number of items in this Bag which elicits a truthy return value from the passed function.

ParameterTypeDescription
fnfunction

A function, which, when passed an item, returns true to select it as the item to return.

Returns: Number -

The number of matched items.

Extracts the matching items from this Bag into an array based upon the passed value filter function.

ParameterTypeDescription
fnfunction

A function, which, when passed an item, returns a true to place into the resulting array.

thisObjObject

The this reference when the function is called.

Returns: Object[] -

An array of values extracted from this Bag.

Returns nth item in this Bag which elicits a truthy return value from the provided matcher function fn.

ParameterTypeDescription
fnfunction

A function which, when passed an item, returns true to select the item as a match.

nthNumber

The index of the matching item. By default, 0 returns the first item that matches according to fn. Negative numbers index for the last item. For example, -1 returns the last matching item, -2 the 2nd to last matching item etc..

Returns: Object -

The matched item, or undefined.

Executes the passed function for each item in this Bag, passing in the item.

ParameterTypeDescription
fnfunction

The function to execute.

thisObjObject

The this reference when the function is called.

Returns the item with the passed id.

ParameterTypeDescription
id*

The id to find.

Returns: Object -

The found item, or undefined.

Returns true if this Collection includes an item with the same id as the passed item.

ParameterTypeDescription
itemObject | String | Number

The item to find, or an id to find.

Returns: Boolean -

True if the passed item is found.

Extracts the content of this Bag into an array based upon the passed value extraction function.

ParameterTypeDescription
fnfunction

A function, which, when passed an item, returns a value to place into the resulting array.

thisObjObject

The this reference when the function is called.

Returns: Object[] -

An array of values extracted from this Bag.

Removes the passed item(s) from this Bag.

ParameterTypeDescription
toRemoveObject | Object[]

Item(s) to remove.

Sort the values of this Bag using the passed comparison function.

Setting this property replaces the data set.

ParameterTypeDescription
fnfunction

Comparison function which returns -ve, 0, or +ve