Categories

Compact Array

Remove falsey values from array using lodash _.compact

Array Compaction Tool

This tool removes falsey values from arrays using Lodash's _.compact method.

Falsey values removed:

  • false
  • null
  • 0
  • "" (empty string)
  • undefined
  • NaN

Example:

Input:  [0, 1, false, 2, "", 3, null, 4, undefined, 5, NaN]
Output: [1, 2, 3, 4, 5]

Options:

  • Preserve Zeros: Keep zero values in the array
  • Preserve Empty Strings: Keep empty strings in the array

Enter a JSON array to compact

Keep zero (0) values in the array

Keep empty string ("") values in the array

Key Facts

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

Overview

The Compact Array tool removes falsey values from JSON arrays using Lodash's _.compact method. It cleans data by eliminating false, null, 0, empty strings, undefined, and NaN, with options to preserve zeros and empty strings if needed.

When to Use

  • When you have an array with mixed data types and need to filter out invalid or empty entries.
  • Before performing data analysis or visualization to ensure only meaningful values are included.
  • When cleaning user input or API responses that may contain falsey values.

How It Works

  • Enter a valid JSON array into the input field.
  • Optionally, check 'Preserve Zeros' to keep zero values or 'Preserve Empty Strings' to keep empty strings.
  • Click the compact button to generate a new array with falsey values removed.
  • The output is displayed as a JSON array ready for use.

Use Cases

Cleaning data arrays from databases or APIs for further processing.
Preprocessing arrays in data pipelines to remove noise and invalid entries.
Simplifying JavaScript arrays by eliminating unnecessary falsey values.

Examples

1. Clean a Mixed Data Array

Background
A developer has an array from a JSON response containing numbers, booleans, and nulls.
Problem
The array has falsey values that interfere with calculations.
How to Use
Input the array [0, 1, false, 2, "", 3, null, 4, undefined, 5, NaN] and click compact.
Outcome
The output is [1, 2, 3, 4, 5], with all falsey values removed.

2. Preserve Zeros in Sensor Data

Background
An IoT application logs temperature readings where zero is a valid value indicating freezing point.
Problem
Need to remove nulls and undefined values but keep zeros for accurate analysis.
How to Use
Input the array [23, null, 0, 15, undefined, 0, 12] and check 'Preserve Zeros' before compacting.
Outcome
The output is [23, 0, 15, 0, 12], with zeros retained and other falsey values removed.

Try with Samples

json, video, text

Related Hubs

FAQ

What falsey values are removed?

The tool removes false, null, 0, empty strings, undefined, and NaN.

Can I keep zero values in the array?

Yes, enable the 'Preserve Zeros' option to retain zeros.

How do I preserve empty strings?

Check the 'Preserve Empty Strings' option before compacting.

What input format is required?

The input must be a valid JSON array, e.g., [1, null, 2, false, 3].

Is the output a new array?

Yes, the tool returns a new array with falsey values removed, leaving the original unchanged.

API Documentation

Request Endpoint

POST /en/api/tools/compact-array

Request Parameters

Parameter Name Type Required Description
array textarea Yes Enter a JSON array to compact
preserveZeros checkbox No Keep zero (0) values in the array
preserveEmptyStrings checkbox No Keep empty string ("") values in the array

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-compact-array": {
      "name": "compact-array",
      "description": "Remove falsey values from array using lodash _.compact",
      "baseUrl": "https://elysiatools.com/mcp/sse?toolId=compact-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]