Skip to main content

Slice

Extracts a portion of an array between specified start and end indices.

Common Properties

  • Name - The custom name of the node.
  • Color - The custom color of the node.
  • Delay Before (sec) - Waits in seconds before executing the node.
  • Delay After (sec) - Waits in seconds after executing node.
  • Continue On Error - Automation will continue regardless of any error. The default value is false.
info

If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.

Inputs

  • Array - The array to slice.
  • Start Indice - The zero-based index at which to begin extraction (inclusive).
  • End Indice - The zero-based index at which to end extraction (exclusive). Use 0 to slice to the end of the array.

Outputs

  • Array - The extracted portion of the array.

How It Works

The Slice node extracts a section of an array without modifying the original. When executed, the node:

  1. Receives an array through the Array input
  2. Validates that the array is not null
  3. Receives start and end indices (optional, with defaults)
  4. Validates that indices are within valid bounds
  5. Extracts elements from start index (inclusive) to end index (exclusive)
  6. Returns the extracted portion as a new array

Requirements

  • Valid array as input (not null)
  • Start index must be 0 or greater, and not exceed array length
  • End index must be 0 or greater, and not exceed array length
  • At least one of Start or End index must be non-zero

Index Behavior

  • Start Indice: Where to begin extraction (inclusive)
    • Index 0 = first element
    • Default: 0 if not specified
  • End Indice: Where to stop extraction (exclusive)
    • The element at this index is NOT included
    • Use 0 to slice to the end of the array
    • Default: array length if not specified

Error Handling

The node will return specific errors in the following cases:

  • Array is null: "Array cannot be null"
  • Start index is negative: "Start index cannot be less than zero"
  • Start index too large: "Start index X cannot be higher than the array length Y"
  • End index is negative: "End index cannot be less than zero"
  • End index too large: "End index X cannot be higher than the array length Y"
  • Both indices are zero: "Either start or end index should not be zero"

Usage Examples

Example 1: Extract First N Elements

Get the first 3 elements from an array:

  1. Array: ["A", "B", "C", "D", "E", "F"]
  2. Start Indice: 0
  3. End Indice: 3
  4. Result: ["A", "B", "C"]

Example 2: Extract Last N Elements

Get the last 2 elements:

  1. Array: ["A", "B", "C", "D", "E"] (length = 5)
  2. Start Indice: 3 (length - 2)
  3. End Indice: 0 (means end of array)
  4. Result: ["D", "E"]

Example 3: Extract Middle Section

Get elements from the middle:

  1. Array: [10, 20, 30, 40, 50, 60, 70]
  2. Start Indice: 2
  3. End Indice: 5
  4. Result: [30, 40, 50]

Example 4: Skip First N Elements

Get all elements except the first 2:

  1. Array: ["Skip1", "Skip2", "Keep1", "Keep2", "Keep3"]
  2. Start Indice: 2
  3. End Indice: 0 (end of array)
  4. Result: ["Keep1", "Keep2", "Keep3"]

Example 5: Batch Processing

Process data in batches of 10:

  1. Array: {{flow.allRecords}} (100 records)
  2. Batch 1: Start = 0, End = 10 → records 0-9
  3. Batch 2: Start = 10, End = 20 → records 10-19
  4. Batch 3: Start = 20, End = 30 → records 20-29
  5. Continue until all records processed

Example 6: Pagination

Implement page-based data access:

  1. Array: {{msg.searchResults}} (50 results)
  2. Page size: 10 results per page
  3. Page 1: Start = 0, End = 10 → results 1-10
  4. Page 2: Start = 10, End = 20 → results 11-20
  5. Page 3: Start = 20, End = 30 → results 21-30

Example 7: Remove First and Last Elements

Get middle elements only:

  1. Array: ["First", "A", "B", "C", "Last"] (length = 5)
  2. Start Indice: 1
  3. End Indice: 4 (length - 1)
  4. Result: ["A", "B", "C"]

Tips for Effective Use

  • End index is exclusive: Element at end index is NOT included
  • End = 0 means end of array: Useful for "from index to end" operations
  • Original array unchanged: Slice creates a new array without modifying the original
  • Use Array.GetLength to calculate dynamic indices
  • For pagination: Start = page × pageSize, End = Start + pageSize
  • For batching: Calculate batch boundaries using batch index and size
  • Combine with loops for processing large arrays in chunks
  • Slice is efficient and doesn't require copying the entire array
  • Use to extract ranges based on conditions or calculations

Common Use Cases

  • Extracting specific ranges from arrays
  • Batch processing large datasets
  • Implementing pagination for results
  • Skipping header or footer elements
  • Creating data windows for analysis
  • Splitting arrays into chunks
  • Extracting samples from datasets
  • Processing arrays in manageable portions
  • Implementing "load more" functionality
  • Creating rolling windows for time-series data

Index Calculation Examples

Get Last N Elements

// To get last 5 elements
Start = arrayLength - 5
End = 0 (or arrayLength)

Get All Except First N

// To skip first 3 elements
Start = 3
End = 0

Get All Except Last N

// To exclude last 2 elements
Start = 0
End = arrayLength - 2

Pagination Formula

// Page-based slicing (0-indexed pages)
pageSize = 10
Start = currentPage × pageSize
End = Start + pageSize

Batch Processing Formula

// For batch N (0-indexed batches)
batchSize = 50
Start = batchIndex × batchSize
End = Start + batchSize

Integration Examples

Example: Batch Processing Pipeline

1. Get total data: {{flow.allData}}
2. Get array length → {{flow.totalCount}}
3. Set batch size: {{flow.batchSize}} = 100
4. Calculate total batches: Math.ceil(totalCount / batchSize)
5. For each batch (i = 0 to totalBatches - 1):
a. Calculate start: i × batchSize
b. Calculate end: start + batchSize
c. Slice array with start and end
d. Process batch
e. Log progress

Example: Pagination Implementation

1. Get search results: {{msg.results}}
2. Set page size: {{flow.pageSize}} = 20
3. Get current page: {{msg.currentPage}}
4. Calculate indices:
Start = currentPage × pageSize
End = Start + pageSize
5. Slice to get current page results
6. Display results to user
7. Provide next/previous page options

Example: Rolling Window Analysis

1. Get time-series data: {{flow.dataPoints}}
2. Set window size: {{flow.windowSize}} = 10
3. For each position (i = 0 to length - windowSize):
a. Start = i
b. End = i + windowSize
c. Slice to get window data
d. Calculate statistics for window
e. Store result
4. Result: Moving average or rolling calculations

Example: Extract Sample Data

1. Get full dataset: {{flow.fullData}}
2. Calculate sample indices:
Start = 100 (skip first 100 records)
End = 200 (get next 100 records)
3. Slice to extract sample
4. Use sample for testing or validation
5. Apply results to full dataset

Common Errors and Solutions

Error: "Array cannot be null"

Cause: The array input is null or undefined.

Solutions:

  • Verify that the array variable exists and is initialized
  • Check that previous nodes successfully created the array

Error: "Start index cannot be less than zero"

Cause: The start index is negative.

Solutions:

  • Ensure start index is 0 or greater
  • Check calculation logic for dynamic indices

Error: "Start index X cannot be higher than the array length Y"

Cause: The start index exceeds the array length.

Solutions:

  • Use Array.GetLength to validate index before slicing
  • Add bounds checking: if (start < length) then slice
  • Adjust calculation logic for dynamic start indices

Error: "End index cannot be less than zero"

Cause: The end index is negative.

Solutions:

  • Ensure end index is 0 or greater
  • Remember: 0 means "end of array" (not an error)
  • Check calculation logic for dynamic end indices

Error: "End index X cannot be higher than the array length Y"

Cause: The end index exceeds the array length.

Solutions:

  • Use Array.GetLength to validate index
  • Use 0 for end index to always slice to the end
  • Adjust calculation: End = Math.min(calculatedEnd, arrayLength)

Error: "Either start or end index should not be zero"

Cause: Both start and end indices are set to 0.

Solutions:

  • Set start to 0 and end to non-zero value, OR
  • Set start to non-zero value and end to 0, OR
  • Set both to non-zero values
  • At least one index must be specified