Extends
Members
The number of values in the collection.
Type:
number
Whether null
is a valid value for the collection.
Type:
boolean
The type of values in the collection.
Type:
string
Methods
- property
- Type:
string
The name of the property.
- value
- Type:
string
The updated property value.
-
Error
If no property with the name exists.
Bulk update objects in the collection.
Parameters:
Throws:
Inherited Methods
- callback
- Type:
function
A function to be called when changes occur. The callback function is called with two arguments:
collection
: the collection instance that changed,changes
: a dictionary with keysinsertions
,newModifications
,oldModifications
anddeletions
, each containing a list of indices in the collection that were inserted, updated or deleted respectively.deletions
andoldModifications
are indices into the collection before the change happened, whileinsertions
andnewModifications
are indices into the new version of the collection.
-
Error
If
callback
is not a function.
- property optional
- Type:
string
For a collection of objects, the property to take the average of.
-
Error
If no property with the name exists or if property is not numeric.
- callback
- Type:
function
Function to execute on each object in the collection. If this function returns
true
for every object, then this method will returntrue
. This function takes three arguments:object
– The current object being processed in the collection.index
– The index of the object being processed in the collection.collection
– The collection itself.
- thisArg optional
- Type:
object
The value of
this
whencallback
is called.- query
- Type:
string
Query used to filter objects from the collection.
- arg optional repeatable
- Type:
any
Each subsequent argument is used by the placeholders (e.g.
$0
,$1
,$2
, …) in the query.-
Error
If the query or any other argument passed into this method is invalid.
- callback
- Type:
function
Function to execute on each object in the collection. If this function returns
true
, then that object will be returned by this method. This function takes three arguments:object
– The current object being processed in the collection.index
– The index of the object being processed in the collection.collection
– The collection itself.
- thisArg optional
- Type:
object
The value of
this
whencallback
is called.- callback
- Type:
function
Function to execute on each object in the collection. If this function returns
true
, then the index of that object will be returned by this method. This function takes three arguments:object
– The current object being processed in the collection.index
– The index of the object being processed in the collection.collection
– The collection itself.
- thisArg optional
- Type:
object
The value of
this
whencallback
is called.- callback
- Type:
function
Function to execute on each object in the collection. This function takes three arguments:
object
– The current object being processed in the collection.index
– The index of the object being processed in the collection.collection
– The collection itself.
- thisArg optional
- Type:
object
The value of
this
whencallback
is called.- object
- Type:
T
The value to search for in the collection.
-
Error
If the argument is a
Realm.Object
that does not belong to the same Realm as the collection.
- separator optional
- Type:
string
- Default:
","
A string to separate the return values of the
toString()
method being called on each object in the collection.- callback
- Type:
function
Function to execute on each object in the collection. This function takes three arguments:
object
– The current object being processed in the collection.index
– The index of the object being processed in the collection.collection
– The collection itself.
- thisArg optional
- Type:
object
The value of
this
whencallback
is called.- property optional
- Type:
string
For a collection of objects, the property to take the maximum of.
-
Error
If no property with the name exists or if property is not numeric/date.
- property optional
- Type:
string
For a collection of objects, the property to take the minimum of.
-
Error
If no property with the name exists or if property is not numeric/date.
- callback
- Type:
function
Function to execute on each object in the collection. This function takes four arguments:
previousValue
– The value previously returned in the last invocation of the callback, orinitialValue
, if supplied.object
– The current object being processed in the collection.index
– The index of the object being processed in the collection.collection
– The collection itself.
- initialValue optional
- Type:
object
The value to use as the first argument to the first call of the
callback
.-
TypeError
If the collection is empty and no
initialValue
was supplied.
- If collection consists of a single object, and no
initalValue
was supplied, then that object will be returned. - If the collection is empty, then
initialValue
must be supplied and will be returned. - callback
- Type:
function
Function to execute on each object, from right to left, in the collection. This function takes four arguments:
previousValue
– The value previously returned in the last invocation of the callback, orinitialValue
, if supplied.object
– The current object being processed in the collection.index
– The index of the object being processed in the collection.collection
– The collection itself.
- initialValue optional
- Type:
object
The value to use as the first argument to the first call of the
callback
.-
TypeError
If the collection is empty and no
initialValue
was supplied.
- If collection consists of a single object, and no
initalValue
was supplied, then that object will be returned. - If the collection is empty, then
initialValue
must be supplied and will be returned. - callback
- Type:
function
Callback function that was previously added as a listener through the
addListener
method.-
Error
If
callback
is not a function.
- start optional
- Type:
number
- Default:
0
The start index. If negative, then the start index will be counted from the end of the collection.
- end optional
- Type:
number
The end index. The objects up to, but not including, the end index will be include in the return value. If negative, then the end index will be counted from the end of the collection. If omitted, then all objects from the start index will be included in the return value.
-
Error
When snapshotting a collection of primitive types.
- callback
- Type:
function
Function to execute on each object in the collection. If this function ever returns
true
, then this method will returntrue
. This function takes three arguments:object
– The current object being processed in the collection.index
– The index of the object being processed in the collection.collection
– The collection itself.
- thisArg optional
- Type:
object
The value of
this
whencallback
is called.- descriptor optional
- Type:
string
or[SortDescriptor, ...]
The property name(s) to sort the collection on.
- reverse optional
- Type:
boolean
- Default:
false
Sort in descending order rather than ascended. May not be supplied if
descriptor
is an array of sort descriptors.-
Error
If a specified property does not exist.
- property optional
- Type:
string
For a collection of objects, the property to take the sum of.
-
Error
If no property with the name exists or if property is not numeric.
Iterator<T>
This is the same method as the values()
method.
Its presence makes collections iterable, thus able to be used with ES6
for-of
loops,
...
spread operators, and more.
Iterator<T>
of each Realm object in the collection
Example:
for (let object of collection) {
// do something with each object
}
Add a listener callback
which will be called when a live collection instance changes.
Parameters:
Throws:
Example:
wines.addListener((collection, changes) => {
// collection === wines
console.log(`${changes.insertions.length} insertions`);
console.log(`${changes.modifications.length} modifications`);
console.log(`${changes.deletions.length} deletions`);
console.log(`new size of collection: ${collection.length}`);
});
number
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:
Throws:
number
the sum.
boolean
Parameters:
boolean
representing if callback
returned true
for every object in the
collection.
Realm.Results<T>
Returns new Results that represent this collection being filtered by the provided query.
Parameters:
Throws:
Realm.Results<T>
filtered according to the provided query.
This is currently only supported for collections of Realm Objects.
See Query language for details about the query language.
Example:
let merlots = wines.filtered('variety == "Merlot" && vintage <= $0', maxYear);
T
or undefined
Parameters:
T
or undefined
if the callback
did not return true
for any object
in the collection.
number
Parameters:
number
representing the index where the callback
returned true
, or -1
if true
was never returned.
Parameters:
number
Finds the index of the given object in the collection.
Parameters:
Throws:
number
representing the index where the value was found, or
-1
if not in collection.
boolean
Checks if this collection is empty.
Returns:boolean
indicating if the collection is empty or not.
boolean
Checks if this collection has not been deleted and is part of a valid Realm.
Returns:boolean
indicating if the collection can be safely accessed.
string
Joins all objects in the collection into a string.
Parameters:
string
Iterator<T>
Iterator<T>
of each index in the collection
[any, ...]
Parameters:
[any, ...]
– the return values of callback
after being called on every object
in the collection.
number
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:
Throws:
number
the maximum value.
number
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:
Throws:
number
the minimum value.
any
Parameters:
Throws:
any
– the value returned by the final invocation of callback
, except for
the following special cases:
any
Parameters:
Throws:
any
– the value returned by the final invocation of callback
, except for
the following special cases:
Remove all callback
listeners from the collection instance.
Remove the listener callback
from the collection instance.
Parameters:
Throws:
[T, ...]
Parameters:
[T, ...]
containing the objects from the start index up to, but not
including, the end index.
Realm.Results<T>
Create a 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.
Throws:
Realm.Results<T>
which will not live update.
boolean
Parameters:
boolean
– true
when callback
returns true
for an object in the collection,
otherwise false
.
Realm.Results<T>
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:
Throws:
Realm.Results<T>
sorted according to the arguments passed in.
Examples::
// Sort wines by age
wines.sorted('age')
// Sort wines by price in descending order, then sort ties by age in
// ascending order
wines.sorted([['price', false], ['age']])
// Sort a list of numbers in ascending order
let sortedPrices = wine.pricesSeen.sort()
// Sort people by how expensive their favorite wine is
people.sort("favoriteWine.price")
number
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:
Throws:
number
the sum.