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
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, textRelated 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.