Instances of this class will be returned when accessing object properties whose type is "Set"

Sets mostly behave like normal JavaScript Sets, with a few exceptions: They can only store values of a single type (indicated by the type and optional properties of the Set). They can only be modified inside a write transaction. Unlike JavaScript's Set, Realm~Set does NOT make any guarantees about the traversal order of values(), entries(), keys(), or forEach iterations. If values in a Set are required to have some order, it must be implemented by the developer by, for example, wrapping values in an object that holds a user-supplied insertion order.

Type Parameters

  • T = unknown

Hierarchy (view full)

Properties

[unscopables]: {
    [unscopables]?: boolean;
    length?: boolean;
    [iterator]?: any;
    at?: any;
    concat?: any;
    copyWithin?: any;
    entries?: any;
    every?: any;
    fill?: any;
    filter?: any;
    find?: any;
    findIndex?: any;
    flat?: any;
    flatMap?: any;
    forEach?: any;
    includes?: any;
    indexOf?: any;
    join?: any;
    keys?: any;
    lastIndexOf?: any;
    map?: any;
    pop?: any;
    push?: any;
    reduce?: any;
    reduceRight?: any;
    reverse?: any;
    shift?: any;
    slice?: any;
    some?: any;
    sort?: any;
    splice?: any;
    toLocaleString?: any;
    toString?: any;
    unshift?: any;
    values?: any;
} = ...

An Object whose truthy properties are properties that are excluded from the 'with' environment bindings of the associated objects.

Type declaration

  • Optional Readonly [unscopables]?: boolean

    Is an object whose properties have the value 'true' when they will be absent when used in a 'with' statement.

  • Optional length?: boolean

    Gets or sets the length of the array. This is a number one higher than the highest index in the array.

Accessors

  • get optional(): boolean
  • Whether null is a valid value for the collection.

    Returns boolean

    Whether null is a valid value for the collection.

Methods

  • Add a new value to the Set.

    Parameters

    • value: T

      Value to add to the Set.

    Returns this

    The Set itself, after adding the new value.

    Throws

    A TypeError if a value is not of a type which can be stored in the Set, or if an object being added to the Set does not match the for the Set.

    Throws

    An Error if not inside a write transaction.

  • Add a listener callback which will be called when a live collection instance changes.

    Parameters

    • callback: CollectionChangeCallback<T, [T, T]>

      A function to be called when changes occur.

    • Optional keyPaths: string | string[]

      Indicates a lower bound on the changes relevant for the listener. This is a lower bound, since if multiple listeners are added (each with their own keyPaths) the union of these key-paths will determine the changes that are considered relevant for all listeners registered on the collection. In other words: A listener might fire more than the key-paths specify, if other listeners with different key-paths are present.

    Returns void

    Note

    deletions and oldModificationsreport the indices in the collection before the change happened, whileinsertionsandnewModificationsreport the indices into the new version of the collection. @throws A {@link TypeAssertionError} ifcallback is not a function. @example wines.addListener((collection, changes) => { // collection === wines console.log(${changes.insertions.length} insertions); console.log(${changes.oldModifications.length} oldModifications); console.log(${changes.newModifications.length} newModifications); console.log(${changes.deletions.length} deletions); console.log(new size of collection: ${collection.length}); }); @example wines.addListener((collection, changes) => { console.log("A wine's brand might have changed"); }, ["brand"]); @note Adding the listener is an asynchronous operation, so the callback is invoked the first time to notify the caller when the listener has been added. Thus, when the callback is invoked the first time it will contain empty arrays for each property in the changes` object.

  • Parameters

    • index: number

    Returns undefined | T

    The element at the given index in the array; undefined if there is no element at the given index.

    See

    Array.prototype.at()

    Params

    index - The index of the element to return from the array. If the index is a negative number, the element at array.length + index is returned.

  • Computes the average of the values in the collection or of the given property among all the objects in the collection, or undefined if the collection is empty.

    Only supported for int, float and double properties. null values are ignored entirely by this method and will not be factored into the average.

    Parameters

    • Optional property: string

      For a collection of objects, the property to take the average of.

    Returns undefined | number

    The sum.

    Throws

    An Error if no property with the name exists or if property is not numeric.

  • Remove all values from the Set.

    Returns void

    Throws

    An Error if not inside a write transaction.

  • Delete a value from the Set.

    Parameters

    • value: T

      Value to delete from the Set.

    Returns boolean

    true if the value existed in the Set prior to deletion, false if not.

    Throws

    An Error if not inside a write transaction.

  • Type Parameters

    • S

    Parameters

    • predicate: ((value, index, array) => value is S)
        • (value, index, array): value is S
        • Parameters

          • value: T
          • index: number
          • array: readonly T[]

          Returns value is S

    • Optional thisArg: any

    Returns this is readonly S[]

    true if the callback function returns a truthy value for every collection element; otherwise, false.

    See

    Array.prototype.every()

    Params

    predicate - A function to test for each element.

    Params

    predicate.value - The current element being processed in the collection.

    Params

    predicate.index - The index of the current element being processed in the collection.

    Params

    predicate.array - The collection every was called upon.

    Params

    thisArg - An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

  • Parameters

    • predicate: ((value, index, array) => unknown)
        • (value, index, array): unknown
        • Parameters

          • value: T
          • index: number
          • array: readonly T[]

          Returns unknown

    • Optional thisArg: any

    Returns boolean

    true if the callback function returns a truthy value for every collection element; otherwise, false.

    See

    Array.prototype.every()

    Params

    predicate - A function to test for each element.

    Params

    predicate.value - The current element being processed in the collection.

    Params

    predicate.index - The index of the current element being processed in the collection.

    Params

    predicate.array - The collection every was called upon.

    Params

    thisArg - An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

  • Type Parameters

    • S

    Parameters

    • predicate: ((value, index, array) => value is S)
        • (value, index, array): value is S
        • Parameters

          • value: T
          • index: number
          • array: readonly T[]

          Returns value is S

    • Optional thisArg: any

    Returns S[]

    A new array containing the elements of the collection for which the predicate function returned true.

    See

    Array.prototype.filter()

    Params

    predicate - A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the collection.

    Params

    predicate.value - The current element being processed in the collection.

    Params

    predicate.index - The index of the current element being processed in the collection.

    Params

    predicate.array - The collection filter was called upon.

    Params

    thisArg - An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

  • Parameters

    • predicate: ((value, index, array) => unknown)
        • (value, index, array): unknown
        • Parameters

          • value: T
          • index: number
          • array: readonly T[]

          Returns unknown

    • Optional thisArg: any

    Returns T[]

    A new array containing the elements of the collection for which the predicate function returned true.

    See

    Array.prototype.filter()

    Params

    predicate - A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the collection.

    Params

    predicate.value - The current element being processed in the collection.

    Params

    predicate.index - The index of the current element being processed in the collection.

    Params

    predicate.array - The collection filter was called upon.

    Params

    thisArg - An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

  • Returns new Results that represent this collection being filtered by the provided query.

    Parameters

    • queryString: string

      Query used to filter objects from the collection.

    • Rest ...args: unknown[]

      Each subsequent argument is used by the placeholders (e.g. $0, $1, $2, …) in the query.

    Returns Results<T>

    Results filtered according to the provided query.

    Throws

    An Error if the query or any other argument passed into this method is invalid.

    Note

    This is currently only supported for collections of Realm Objects.

    Example

    let merlots = wines.filtered('variety == "Merlot" && vintage <= $0', maxYear);
    
  • Type Parameters

    • S

    Parameters

    • predicate: ((this, value, index, obj) => value is S)
        • (this, value, index, obj): value is S
        • Parameters

          • this: void
          • value: T
          • index: number
          • obj: T[]

          Returns value is S

    • Optional thisArg: any

    Returns undefined | S

    The value of the first element in the array that satisfies the provided testing function. Otherwise, undefined is returned.

    See

    Array.prototype.find()

    Params

    predicate - A function that accepts up to three arguments. The find method calls the predicate function one time for each element in the collection.

    Params

    predicate.value - The value of the element.

    Params

    predicate.index - The index of the element.

    Params

    predicate.obj - The object being traversed.

    Params

    thisArg - An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

  • Type Parameters

    • T

    Parameters

    • predicate: ((value, index, obj) => unknown)
        • (value, index, obj): unknown
        • Parameters

          • value: T
          • index: number
          • obj: T[]

          Returns unknown

    • Optional thisArg: any

    Returns undefined | T

    The value of the first element in the array that satisfies the provided testing function. Otherwise, undefined is returned.

    See

    Array.prototype.find()

    Params

    predicate - A function that accepts up to three arguments. The find method calls the predicate function one time for each element in the collection.

    Params

    predicate.value - The value of the element.

    Params

    predicate.index - The index of the element.

    Params

    predicate.obj - The object being traversed.

    Params

    thisArg - An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

  • Parameters

    • predicate: ((value, index, obj) => unknown)
        • (value, index, obj): unknown
        • Parameters

          • value: T
          • index: number
          • obj: readonly T[]

          Returns unknown

    • Optional thisArg: any

    Returns number

    The index of the first element in the array that satisfies the provided testing function. Otherwise, -1 is returned.

    See

    Array.prototype.findIndex()

    Params

    predicate - A function that accepts up to three arguments. The findIndex method calls the predicate function one time for each element in the collection.

    Params

    predicate.value - The value of the element.

    Params

    predicate.index - The index of the element.

    Params

    predicate.obj - The object being traversed.

    Params

    thisArg - An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

  • Type Parameters

    • U

    • This = undefined

    Parameters

    • callback: ((this, value, index, array) => U | readonly U[])
        • (this, value, index, array): U | readonly U[]
        • Parameters

          • this: This
          • value: T
          • index: number
          • array: T[]

          Returns U | readonly U[]

    • Optional thisArg: This

    Returns U[]

    A new array with each element being the result of the callback function and flattened to a depth of 1.

    See

    Array.prototype.flatMap()

    Params

    callback - Function that produces an element of the new Array, taking three arguments:

    Params

    callback.currentValue - The current element being processed in the array.

    Params

    callback.index - The index of the current element being processed in the array.

    Params

    callback.array - The array flatMap was called upon.

    Params

    thisArg - Value to use as this when executing callback.

  • Parameters

    • callbackfn: ((value, index, array) => void)
        • (value, index, array): void
        • Parameters

          • value: T
          • index: number
          • array: readonly T[]

          Returns void

    • Optional thisArg: any

    Returns void

    See

    Array.prototype.forEach()

    Params

    callbackfn - A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the collection.

    Params

    callbackfn.value - The current element being processed in the collection.

    Params

    callbackfn.index - The index of the current element being processed in the collection.

    Params

    callbackfn.array - The collection forEach was called upon.

    Params

    thisArg - An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

  • Check for existence of a value in the Set.

    Parameters

    • value: T

      Value to search for in the Set

    Returns boolean

    true if the value exists in the Set, false if not.

    Throws

    A TypeError if a value is not of a type which can be stored in the Set, or if an object being added to the Set does not match the object schema for the Set.

  • Parameters

    • searchElement: T
    • Optional fromIndex: number

    Returns boolean

    true if the searchElement is found in the array; otherwise, false.

    See

    Array.prototype.includes()

    Params

    searchElement - The element to search for.

    Params

    fromIndex - The position in this array at which to begin searching for searchElement. A negative value searches from the index of array.length + fromIndex by asc.

    Note

    fromIndex is currently not supported. So all searches start at index 0.

  • Parameters

    • searchElement: T
    • Optional fromIndex: number

    Returns number

    The first index at which a given element can be found in the collection, or -1 if it is not present.

    See

    Array.prototype.indexOf()

    Params

    searchElement - Element to locate in the collection.

    Params

    fromIndex - The collection index at which to begin the search. If omitted, the search starts at index 0.

    Note

    fromIndex is currently not supported. So all searches start at index 0.

  • Parameters

    • searchElement: T
    • Optional fromIndex: number

    Returns number

    The last index at which a given element can be found in the collection, or -1 if it is not present. The collection is searched backwards, starting at fromIndex.

    See

    Array.prototype.lastIndexOf()

    Params

    searchElement - Element to locate in the collection.

    Params

    fromIndex - The collection index at which to begin the search. If omitted, the search starts at the last index.

  • Type Parameters

    • U

    Parameters

    • callbackfn: ((value, index, array) => U)
        • (value, index, array): U
        • Parameters

          • value: T
          • index: number
          • array: readonly T[]

          Returns U

    • Optional thisArg: any

    Returns U[]

    A new array containing the results of calling the callbackfn function on each element in the collection.

    See

    Array.prototype.map()

    Params

    callbackfn - A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the collection.

    Params

    callbackfn.value - The current element being processed in the collection.

    Params

    callbackfn.index - The index of the current element being processed in the collection.

    Params

    callbackfn.array - The collection map was called upon.

    Params

    thisArg - An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.

  • Returns the maximum value of the values in the collection or of the given property among all the objects in the collection, or undefined if the collection is empty.

    Only supported for int, float, double and date properties. null values are ignored entirely by this method and will not be returned.

    Parameters

    • Optional property: string

      For a collection of objects, the property to take the maximum of.

    Returns undefined | number | Date

    The maximum value.

    Throws

    An Error if no property with the name exists or if property is not numeric/date.

  • Returns the minimum value of the values in the collection or of the given property among all the objects in the collection, or undefined if the collection is empty.

    Only supported for int, float, double and date properties. null values are ignored entirely by this method and will not be returned.

    Parameters

    • Optional property: string

      For a collection of objects, the property to take the minimum of.

    Returns undefined | number | Date

    The minimum value.

    Throws

    A TypeAssertionError if no property with the name exists or if property is not numeric/date.

  • Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => T)
        • (previousValue, currentValue, currentIndex, array): T
        • Parameters

          • previousValue: T
          • currentValue: T
          • currentIndex: number
          • array: readonly T[]

          Returns T

    Returns T

    The value that results from the reduction.

    See

    Array.prototype.reduce()

    Params

    callbackfn - A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the collection.

    Params

    callbackfn.previousValue - The value previously returned in the last invocation of the callbackfn function, or initialValue, if supplied. (See below.)

    Params

    callbackfn.currentValue - The current element being processed in the collection.

    Params

    callbackfn.currentIndex - The index of the current element being processed in the collection.

    Params

    callbackfn.array - The collection reduce was called upon.

    Params

    initialValue - If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an element value.

  • Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => T)
        • (previousValue, currentValue, currentIndex, array): T
        • Parameters

          • previousValue: T
          • currentValue: T
          • currentIndex: number
          • array: readonly T[]

          Returns T

    • initialValue: T

    Returns T

    The value that results from the reduction.

    See

    Array.prototype.reduce()

    Params

    callbackfn - A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the collection.

    Params

    callbackfn.previousValue - The value previously returned in the last invocation of the callbackfn function, or initialValue, if supplied. (See below.)

    Params

    callbackfn.currentValue - The current element being processed in the collection.

    Params

    callbackfn.currentIndex - The index of the current element being processed in the collection.

    Params

    callbackfn.array - The collection reduce was called upon.

    Params

    initialValue - If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an element value.

  • Type Parameters

    • U

    Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => U)
        • (previousValue, currentValue, currentIndex, array): U
        • Parameters

          • previousValue: U
          • currentValue: T
          • currentIndex: number
          • array: readonly T[]

          Returns U

    • initialValue: U

    Returns U

    The value that results from the reduction.

    See

    Array.prototype.reduce()

    Params

    callbackfn - A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the collection.

    Params

    callbackfn.previousValue - The value previously returned in the last invocation of the callbackfn function, or initialValue, if supplied. (See below.)

    Params

    callbackfn.currentValue - The current element being processed in the collection.

    Params

    callbackfn.currentIndex - The index of the current element being processed in the collection.

    Params

    callbackfn.array - The collection reduce was called upon.

    Params

    initialValue - If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an element value.

  • Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => T)
        • (previousValue, currentValue, currentIndex, array): T
        • Parameters

          • previousValue: T
          • currentValue: T
          • currentIndex: number
          • array: readonly T[]

          Returns T

    Returns T

    The value that results from the reduction.

    See

    Array.prototype.reduceRight()

    Params

    callbackfn - A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the collection.

    Params

    callbackfn.previousValue - The value previously returned in the last invocation of the callbackfn function, or initialValue, if supplied. (See below.)

    Params

    callbackfn.currentValue - The current element being processed in the collection.

    Params

    callbackfn.currentIndex - The index of the current element being processed in the collection.

    Params

    callbackfn.array - The collection reduceRight was called upon.

    Params

    initialValue - If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an element value.

  • Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => T)
        • (previousValue, currentValue, currentIndex, array): T
        • Parameters

          • previousValue: T
          • currentValue: T
          • currentIndex: number
          • array: readonly T[]

          Returns T

    • initialValue: T

    Returns T

    The value that results from the reduction.

    See

    Array.prototype.reduceRight()

    Params

    callbackfn - A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the collection.

    Params

    callbackfn.previousValue - The value previously returned in the last invocation of the callbackfn function, or initialValue, if supplied. (See below.)

    Params

    callbackfn.currentValue - The current element being processed in the collection.

    Params

    callbackfn.currentIndex - The index of the current element being processed in the collection.

    Params

    callbackfn.array - The collection reduceRight was called upon.

    Params

    initialValue - If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an element value.

  • Type Parameters

    • U

    Parameters

    • callbackfn: ((previousValue, currentValue, currentIndex, array) => U)
        • (previousValue, currentValue, currentIndex, array): U
        • Parameters

          • previousValue: U
          • currentValue: T
          • currentIndex: number
          • array: readonly T[]

          Returns U

    • initialValue: U

    Returns U

    The value that results from the reduction.

    See

    Array.prototype.reduceRight()

    Params

    callbackfn - A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the collection.

    Params

    callbackfn.previousValue - The value previously returned in the last invocation of the callbackfn function, or initialValue, if supplied. (See below.)

    Params

    callbackfn.currentValue - The current element being processed in the collection.

    Params

    callbackfn.currentIndex - The index of the current element being processed in the collection.

    Params

    callbackfn.array - The collection reduceRight was called upon.

    Params

    initialValue - If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an element value.

  • Create a frozen snapshot of the collection.

    Values added to and removed from the original collection will not be reflected in the Results returned by this method, including if the values of properties are changed to make them match or not match any filters applied.

    This is not a deep snapshot. Realm objects contained in this snapshot will continue to update as changes are made to them, and if they are deleted from the Realm they will be replaced by null at the respective indices.

    Returns Results<T>

    Results which will not live update.

  • Parameters

    • predicate: ((value, index, array) => unknown)
        • (value, index, array): unknown
        • Parameters

          • value: T
          • index: number
          • array: readonly T[]

          Returns unknown

    • Optional thisArg: any

    Returns boolean

    true if the callback function returns a truthy value for any collection element; otherwise, false.

    See

    Array.prototype.some()

    Params

    predicate - A function to test for each element.

    Params

    predicate.value - The current element being processed in the collection.

    Params

    predicate.index - The index of the current element being processed in the collection.

    Params

    predicate.array - The collection every was called upon.

    Params

    thisArg - An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.

  • Returns new Results that represent a sorted view of this collection.

    A collection of Realm Objects can be sorted on one or more properties of those objects, or of properties of objects linked to by those objects. To sort by a single property, simply pass the name of that property to sorted(), optionally followed by a boolean indicating if the sort should be reversed. For more than one property, you must pass an array of sort descriptors which list which properties to sort on.

    Collections of other types sort on the values themselves rather than properties of the values, and so no property name or sort descriptors should be supplied.

    Parameters

    • Optional reverse: boolean

      Sort in descending order rather than ascended. It may not be applied if descriptor is an array of sort descriptors.

    Returns Results<T>

    Results sorted according to the arguments passed in.

    Throws

    An Error if a specified property does not exist.

  • Returns new Results that represent a sorted view of this collection.

    A collection of Realm Objects can be sorted on one or more properties of those objects, or of properties of objects linked to by those objects. To sort by a single property, simply pass the name of that property to sorted(), optionally followed by a boolean indicating if the sort should be reversed. For more than one property, you must pass an array of sort descriptors which list which properties to sort on.

    Collections of other types sort on the values themselves rather than properties of the values, and so no property name or sort descriptors should be supplied.

    Parameters

    Returns Results<T>

    Results sorted according to the arguments passed in.

    Throws

    An Error if a specified property does not exist.

  • Returns new Results that represent a sorted view of this collection.

    A collection of Realm Objects can be sorted on one or more properties of those objects, or of properties of objects linked to by those objects. To sort by a single property, simply pass the name of that property to sorted(), optionally followed by a boolean indicating if the sort should be reversed. For more than one property, you must pass an array of sort descriptors which list which properties to sort on.

    Collections of other types sort on the values themselves rather than properties of the values, and so no property name or sort descriptors should be supplied.

    Parameters

    • descriptor: string

      The property name(s) to sort the collection on.

    • Optional reverse: boolean

    Returns Results<T>

    Results sorted according to the arguments passed in.

    Throws

    An Error if a specified property does not exist.

  • Computes the sum of the values in the collection or of the given property among all the objects in the collection, or 0 if the collection is empty.

    Only supported for int, float and double properties. null values are ignored entirely by this method.

    Parameters

    • Optional property: string

      For a collection of objects, the property to take the sum of.

    Returns number

    The sum.

    Throws

    An Error if no property with the name exists or if property is not numeric.

Generated using TypeDoc