# Compact Array

Remove falsey values from array using lodash _.compact

> Canonical page: https://elysiatools.com/en/tools/compact-array

- **Category:** Data Processing

- **Keywords:** array, compact, clean, filter, remove, falsey, lodash

## Overview

## 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:**
```json
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

## Inputs

- **JSON Array** (textarea): Enter a JSON array to compact
- **Preserve Zeros** (checkbox): Keep zero (0) values in the array
- **Preserve Empty Strings** (checkbox): Keep empty string ("") values in the array

## 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.

## Frequently asked questions

### 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.

## Related tools

- [BOM Character Remover](https://elysiatools.com/en/tools/data-bom-remover): Remove BOM (Byte Order Mark) characters from text and file content. Perfect for cleaning up text files that have encoding issues, fixing CSV imports, and preparing data for processing. Features: - Detect and remove UTF-8 BOM (EF BB BF) - Detect and remove UTF-16 BOM (FE FF or FF FE) - Detect and remove UTF-32 BOM (00 00 FE FF or FF FE 00 00) - Support multiple input formats - Visual BOM character display - Detailed detection report - Support for batch text processing Common Use Cases: - Fix CSV file import errors - Clean up text file encoding issues - Prepare data for JSON parsing - Fix XML parsing problems - Resolve API data encoding conflicts - Standardize text data format
- [Data Range Limiter](https://elysiatools.com/en/tools/data-range-limiter): Limit numerical values to specified ranges by clipping, filtering, or marking out-of-bounds values. Perfect for data quality control, sensor data cleaning, business rule enforcement, and data preprocessing. Features: - Range clipping (clip values to min/max boundaries) - Range filtering (remove out-of-bounds rows) - Range marking (flag modified values) - Per-column range configuration - Automatic numeric column detection - Multiple handling strategies - Detailed modification reports - Statistical analysis of changes - Business rule enforcement Common Use Cases: - Sensor data validation and cleaning - Machine learning input preparation - Data quality control and validation - Business constraint enforcement - Outlier management and control - Data preprocessing pipelines
- [JSON Key Extractor](https://elysiatools.com/en/tools/json-key-extractor): Extract all keys from JSON objects with multiple output formats. Perfect for analyzing JSON structure, documentation generation, and understanding complex nested objects.
- [Pull Values](https://elysiatools.com/en/tools/pull-values): Remove specific values from an array using lodash _.pull
- [Array Deduplicator](https://elysiatools.com/en/tools/array-deduplicator): Remove duplicate elements from arrays with various deduplication methods
- [Array Filter](https://elysiatools.com/en/tools/array-filter): Filter out elements of specific types from arrays (remove numbers, strings, booleans, etc.)
- [Array Element Deleter](https://elysiatools.com/en/tools/array-element-deleter): Delete specific elements from arrays with support for multiple deletion methods
- [Drop Items](https://elysiatools.com/en/tools/drop-items): Drop first N items from array using lodash _.drop

## Samples

- [Distributed Tracing Samples](https://elysiatools.com/en/samples/distributed-tracing-samples): Comprehensive distributed tracing examples using Jaeger, OpenTelemetry, and other modern observability tools for microservices architecture
- [JWT Samples](https://elysiatools.com/en/samples/jwt-samples): Comprehensive JWT examples from basic token structure to advanced security implementations
- [PDF Samples](https://elysiatools.com/en/samples/pdf-samples): Generated PDF samples from tools dated 2026-02-01 to 2026-02-10
- [WebRTC Real-Time Communication Samples](https://elysiatools.com/en/samples/webrtc-samples): Comprehensive WebRTC samples for peer-to-peer audio/video communication, data channels, screen sharing, and signaling server implementation
