On This Page
Array Helpers
These helpers provide convenient ways to modify arrays reactively, ensuring that dependent computations are updated when the array changes.
Collection Helpers - For arrays of records see collection helpers which handle manipulating arrays of records with unique ids.
Usage Note
Array helpers allow you to update reactive values without having to call get
then an array function like push
and finally set
to triger the reaction.
We’ve also included a few common advanced use cases for arrays like setIndex
removeIndex
and setArrayProperty
to handle replacing a specific element in an array.
push
Adds one or more elements to the end of the array and returns the new length of the array.
Syntax
Parameters
Name | Type | Description |
---|---|---|
…elements | any | One or more elements to add to the end of the array |
Returns
number - The new length of the array.
Usage
unshift
Adds one or more elements to the beginning of the array and returns the new length of the array.
Syntax
Parameters
Name | Type | Description |
---|---|---|
…elements | any | One or more elements to add to the beginning of the array |
Returns
number - The new length of the array.
Usage
splice
Changes the contents of the array by removing or replacing existing elements and/or adding new elements in place.
Syntax
Parameters
Name | Type | Description |
---|---|---|
start | number | The index at which to start changing the array |
deleteCount | number | An integer indicating the number of elements to remove |
…items | any | The elements to add to the array, beginning from start |
Returns
Array - An array containing the deleted elements.
Usage
setIndex
Sets the value at a specific index in the array.
Syntax
Parameters
Name | Type | Description |
---|---|---|
index | number | The index at which to set the value |
value | any | The value to set at the specified index |
Usage
removeIndex
Removes the element at a specific index from the array.
Syntax
Parameters
Name | Type | Description |
---|---|---|
index | number | The index of the element to remove |
Usage
setArrayProperty
Sets a property on an object at a specific index in the array, or on all objects if no index is specified.
Syntax
Parameters
Name | Type | Description |
---|---|---|
indexOrProperty | number or string | The index of the object to modify, or the property name if modifying all objects |
property | string | The name of the property to set (only if first argument is a number) |
value | any | The value to set for the property |