![Logo](/_astro/logo.DNC1PCTe_Z1abT04.webp)
Enter a search term above to see results...
Enter a search term above to see results...
Collection Helpers are methods available on Signal instances that contain arrays of objects. These helpers provide convenient ways to manipulate and update specific objects within the collection based on their unique id.
Unique ids for each row are the first set value in the following list
_id
id
hash
key
If none are set then the record will be ignored
Sets a property on an object in the collection that matches the given id.
signal.setProperty(id, property, value)
Name | Type | Description |
---|---|---|
id | any | The id of the object to modify |
property | string | The name of the property to set |
value | any | The value to set for the property |
import { Signal } from '@semantic-ui/reactivity';
const users = new Signal([ { id: 'user1', name: 'Alice', age: 30 }, { id: 'user2', name: 'Bob', age: 25 }]);
users.setProperty('user2', 'age', 26);console.log(users.get());// Output: [{ id: 1, name: 'Alice', age: 30 }, { id: 2, name: 'Bob', age: 26 }]
Replaces an entire object in the collection that matches the given id.
signal.replaceItem(id, newItem)
Name | Type | Description |
---|---|---|
id | any | The id of the object to replace |
newItem | Object | The new object to replace the old one |
import { Signal } from '@semantic-ui/reactivity';
const tasks = new Signal([ { id: 1, task: 'Buy groceries', completed: false }, { id: 2, task: 'Walk the dog', completed: true }]);
tasks.replaceItem(1, { id: 1, task: 'Buy groceries', completed: true });console.log(tasks.get());// Output: [{ id: 1, task: 'Buy groceries', completed: true }, { id: 2, task: 'Walk the dog', completed: true }]
Removes an object from the collection that matches the given id.
signal.removeItem(id)
Name | Type | Description |
---|---|---|
id | any | The id of the object to remove |
import { Signal } from '@semantic-ui/reactivity';
const books = new Signal([ { id: 1, title: 'The Great Gatsby' }, { id: 2, title: 'To Kill a Mockingbird' }]);
books.removeItem(1);console.log(books.get());// Output: [{ id: 2, title: 'To Kill a Mockingbird' }]
Returns the index of an object in the collection that matches the given id.
signal.getIndex(id)
Name | Type | Description |
---|---|---|
id | any | The id of the object to find |
number - The index of the object, or -1 if not found.
import { Signal } from '@semantic-ui/reactivity';
const fruits = new Signal([ { id: 'a', name: 'apple' }, { id: 'b', name: 'banana' }, { id: 'c', name: 'cherry' }]);
const index = fruits.getIndex('b');console.log(index); // Output: 1
Returns the id of an object at a specific index in the collection.
signal.getID(index)
Name | Type | Description |
---|---|---|
index | number | The index of the object |
any - The id of the object at the specified index, or undefined if the index is out of bounds.
import { Signal } from '@semantic-ui/reactivity';
const animals = new Signal([ { id: 'dog1', species: 'dog' }, { id: 'cat1', species: 'cat' }, { id: 'fish1', species: 'fish' }]);
const id = animals.getID(1);console.log(id); // Output: 'cat1'
Checks if an object at a specific index in the collection has an id.
signal.hasID(index)
Name | Type | Description |
---|---|---|
index | number | The index of the object to check |
boolean - True if the object at the specified index has an id, false otherwise.
import { Signal } from '@semantic-ui/reactivity';
const mixed = new Signal([ { id: 1, value: 'has id' }, { value: 'no id' }, { id: 2, value: 'has id too' }]);
console.log(mixed.hasID(0)); // Output: trueconsole.log(mixed.hasID(1)); // Output: false