Skip to content

INormalizedCollection

Interface | Source Code

A collection structure that combines the benefits of an object (O(1) lookup, stable identifiers) and an array (preservation of order and sorting). Also serializes to JSON nicely provided the data type used for the elements is JSON-serializable.

Whether the collection is currently empty.


The number of items in the collection.

add(item: T, overwriteExisting?: boolean): void

Section titled “add(item: T, overwriteExisting?: boolean): void”

Adds the provided item. If the ID of the item matches an item already in the collection, you can customize behaviour of the method with the overwriteExisting param.


Removes all items from the collection.


filter(predicate: Function): INormalizedCollection

Section titled “filter(predicate: Function): INormalizedCollection”

A new collection that contains only the items from this collection that passed the predicate.


The first occurrence of an item returning true when given to the provided predicate, or undefined if no item passed the predicate.



fromJSON(json: string): undefined | INormalizedCollection

Section titled “fromJSON(json: string): undefined | INormalizedCollection”

A new collection instance that represents the serialized JSON, or undefined if parsing wasn’t possible.


The collection item specified by id, or undefined if there was no item with the provided ID.


Whether the collection includes the item with the specified ID.


includesItem(item: T, comparator?: Function): boolean

Section titled “includesItem(item: T, comparator?: Function): boolean”

Determines whether an item exists in the collection. By default, items are compared by identity (item === otherItem). If that’s insufficient for your use case, use the optional comparator param of the method to specify how equality is determined.

Note that because this method requires traversing the list to find a match, it’s preferable to use includesId when possible.

Whether the collection includes the specified item.


The result of invoking iteratee on every item in the collection.


merge(…others: INormalizedCollection[]): INormalizedCollection

Section titled “merge(…others: INormalizedCollection[]): INormalizedCollection”
// Returns a new collection where
// the contents of `collection`, `otherCollection`, and `anotherCollection` are all present.
// Assuming all collections had an item with ID '123', item '123' in the new collection
// will be the '123' from `anotherCollection` because it was specified last in the merge
// chain.
collection.merge(otherCollection, anotherCollection);

Removes the item specified by the id.


The collection as an array.


The collection serialized as a JSON string.


The collection as a string. If trying to obtain the JSON-serialized version of the collection, use toJSON.