Basic Array Usage
To work with arrays, usefield.state.value on an array field with Svelte’s each blocks.
Simple Array Example
mode="array" prop, which tells the field to operate in array mode.
Adding Items to Arrays
Use thepushValue method to add new items to an array field:
pushValue.
Accessing Nested Fields
Access individual array items using bracket notation in the field name:Complete Array Example
Here’s a full example with adding items and nested field access:Array Field Methods
The field API provides several methods for managing arrays:pushValue(item)
Add an item to the end of the array:removeValue(index)
Remove an item at a specific index:insertValue(index, item)
Insert an item at a specific position:replaceValue(index, item)
Replace an item at a specific index:swapValues(indexA, indexB)
Swap two items in the array:moveValue(from, to)
Move an item from one index to another:clearValues()
Remove all items from the array:Array with Remove Buttons
Add remove buttons to let users delete individual items:Validating Array Fields
You can validate both the array itself and individual array items:Array-Level Validation
Validate the entire array:Item-Level Validation
Validate individual array items:Complex Nested Objects
Arrays can contain complex objects with multiple fields:Empty State
Handle the case when the array is empty using Svelte’selse block:
Array Keying
For better performance with reordering, use Svelte’s keyed each blocks:Next Steps
- Learn more about Form Validation for arrays
- Check the API Reference for all field methods
- Explore Basic Concepts for deeper understanding