ObservableArray
The ObservableArray
class expands the Javascript Array
class by providing a capability of detecting and responding to changes of a collection of objects.
The ObservableArray supports the known methods like concat, push, reduce, slice, splice, reverse and many more (full list here).
Creating an ObservableArray
Creating an ObservableArray with different class constructors.
// Create ObservableArray with lenght
let myObservableArray = new ObservableArray(10)
// Create ObservableArray from array.
const arr = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
myObservableArray = new ObservableArray(arr)
Listening to the ObservableArray.changeEvent
The example in the Stackblitz IDE below shows how to hook to the changeEvent
.
To try it out, you have to download the Nativescript Preview
app from Google Play or App Store.
Once you have the app, scan the QR with your phone Camera and the app resulting from the code in the IDE will appear in the Nativescript Preview
app.
You can use the IDE and the Preview app to experiment with the rest of the ObservableArray
methods.
Array index
One difference between the base array implementation and the ObservableArray
is in the way the items are accessed through their index.
While in the common JS array we would do array[index]
, with an ObservableArray
we need to use the getItem(index)
method.
const firstItem = myObservableArray.getItem(0)
const secondItem = myObservableArray.getItem(1)
const thirdItem = myObservableArray.getItem(2)
console.log(firstItem, secondItem, thirdItem)
Properties
Name | Type | Description |
---|---|---|
|
|
This is a static property used when hooking to change event. |
Methods
Name | Return Type | Description |
---|---|---|
|
|
Sets item at specified index. |
|
|
Returns item at specified index.
|
|
|
Returns a string representation of an array. |
|
|
Combines two or more arrays.
+ |
|
|
Adds all the elements of an array separated by the specified separator string.
+ |
|
|
Removes the last element from an array and returns it. |
|
|
Appends new elements to an array and returns the new length of the array.
+ |
|
|
Reverses the elements in an Array. |
|
|
Removes the first element from an array and returns it. |
|
|
Returns a section of an array.
+ |
|
|
Sorts an array.
+ |
|
|
Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
+ + Parameters: + |
|
|
Inserts new elements at the start of an array and returns the new length of the array.
+ + Parameters: + |
|
|
Returns the index of the first element in the array where predicate is true, and -1 otherwise.
+ + Parameters: + |
|
|
Returns the index of the first occurrence of a value in an array.
+ + Parameters: + |
|
|
Returns the index of the last occurrence of a specified value in an array.
+ + Parameters: + |
|
|
Determines whether all the members of an array satisfy the specified test.
+ + Parameters: + |
|
|
Determines whether the specified callback function returns true for any element of an array.
+ + Parameters: + |
|
|
Determines whether the specified callback function returns true for any element of an array.
+ + Parameters: + |
|
|
Calls the defined callback function on each element of an array and returns an array that contains the results.
+ + Parameters: + |
|
|
Returns the elements of an array that meet the condition specified in a callback function.
+ + Parameters: + |
|
|
Returns the elements of an array that meet the condition specified in a callback function.
+ + Parameters: + |
|
|
Calls the specified callback function for all the elements in an array, in descending order.
The return value of the callback function is the accumulated result and is provided as an argument in the next call to the callback function.
+ + Parameters: + |
|
|
Calls the specified callback function for all the elements in an array starting from the end of the array, in descending order.
The return value of the callback function is the accumulated result and is provided as an argument in the next call to the callback function.
+ + Parameters: + |
For more infomation on what the |