Categories

Slice Array

Extract a section of an array using lodash _.slice

Slice a JSON array using start and end indices.

Highlights:

  • Uses lodash _.slice
  • Supports negative indices
  • Returns the sliced array as JSON

Example:

  • Array: ["a", "b", "c", "d"]
  • Start: 1
  • End: 3
  • Result: ["b", "c"]

Enter a JSON array

Index to start slicing (supports negative)

Index to stop slicing (exclusive)

Key Facts

Category
Data Processing
Input Types
textarea, number
Output Type
text
Sample Coverage
4
API Ready
Yes

Overview

The Slice Array tool extracts a segment from a JSON array using start and end indices. It leverages lodash's _.slice method for accurate slicing, supports negative indices for reverse counting, and outputs the result in JSON format.

When to Use

  • When you need to extract a specific range of elements from a JSON array.
  • When working with arrays where indices might be negative, such as getting the last few items.
  • When automating data processing tasks that involve array manipulation.

How It Works

  • Input a valid JSON array into the designated textarea field.
  • Specify the start index (default is 0) and optionally the end index (exclusive).
  • The tool applies lodash _.slice to the array based on the provided indices.
  • It returns the sliced array as a JSON string for further use.

Use Cases

Extracting a subset of data from a large JSON array for analysis.
Handling API responses where only a portion of the array is needed.
Preparing array data for further processing in scripts or applications.

Examples

1. Extract First Five Elements

Data Scientist
Background
You have a JSON array containing 100 data points and want to examine the first five for initial analysis.
Problem
Manually copying elements is inefficient and prone to mistakes.
How to Use
Enter the array in the input field, set start to 0 and end to 5.
Outcome
The tool returns the first five elements as a JSON array, ready for review.

2. Get Last Three Items with Negative Index

Software Developer
Background
Processing an array from a database query and need the last three records for a summary report.
Problem
Calculating the exact index for the end of the array can be cumbersome.
How to Use
Input the array, set start to -3 and leave end empty or set to null.
Outcome
The tool uses negative indexing to return the last three elements as a JSON array.

Try with Samples

json

Related Hubs

FAQ

What is the default value for the start index?

The default start index is 0.

Can I use negative indices?

Yes, negative indices are supported, counting from the end of the array.

What happens if I don't specify an end index?

If end is not specified, the slice goes to the end of the array.

Is the input array validated?

The tool expects a valid JSON array; invalid input may cause errors.

In what format is the result returned?

The result is returned as a JSON array string.

API Documentation

Request Endpoint

POST /en/api/tools/slice-array

Request Parameters

Parameter Name Type Required Description
array textarea Yes Enter a JSON array
start number No Index to start slicing (supports negative)
end number No Index to stop slicing (exclusive)

Response Format

{
  "result": "Processed text content",
  "error": "Error message (optional)",
  "message": "Notification message (optional)",
  "metadata": {
    "key": "value"
  }
}
Text: Text

AI MCP Documentation

Add this tool to your MCP server configuration:

{
  "mcpServers": {
    "elysiatools-slice-array": {
      "name": "slice-array",
      "description": "Extract a section of an array using lodash _.slice",
      "baseUrl": "https://elysiatools.com/mcp/sse?toolId=slice-array",
      "command": "",
      "args": [],
      "env": {},
      "isActive": true,
      "type": "sse"
    }
  }
}

You can chain multiple tools, e.g.: `https://elysiatools.com/mcp/sse?toolId=png-to-webp,jpg-to-webp,gif-to-webp`, max 20 tools.

If you encounter any issues, please contact us at [email protected]