Syntax, Parameter, and Return Value
The syntax of the _.compact() method is simple and intuitive:
Syntax
_.compact(array)
Parameters
array (Array): The array to be compacted.
Return Value
Array: Returns a new array with all falsy values removed.
Unlike some array methods, _.compact() does not modify the original array; it produces a new array with the filtered content.
Examples of Lodash _.compact() Method
Example 1: Removing Falsy Values
JavaScript
const _ = require('lodash');
const mixedArray = [0, 1, false, 2, '', 3];
const compactedArray = _.compact(mixedArray);
console.log(compactedArray);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 2, 3]
This example illustrates the removal of falsy values like 0, false, and '', leaving only the truthy values.
Example 2: Compact on Array with No Falsy Values
JavaScript
const truthyArray = [1, 2, 3, 4];
const resultArray = _.compact(truthyArray);
console.log(resultArray);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[1, 2, 3, 4]
Here, an array with no falsy values remains unchanged after compacting.
Example 3: Compact on Empty Array
JavaScript
const emptyArray = [];
const compactedEmpty = _.compact(emptyArray);
console.log(compactedEmpty);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[]
Compacting an empty array results in an empty array, demonstrating the method's consistency.
Example 4: Compact on Array with Complex Elements
JavaScript
const complexArray = [null, { name: 'Alice' }, undefined, [1, 2, 3]];
const compactedComplex = _.compact(complexArray);
console.log(compactedComplex);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ name: 'Alice' }, [1, 2, 3]]
In this example, _.compact() effectively removes null and undefined, handling complex data types seamlessly.
Frequently Asked Questions
Does _.compact() affect the original array?
No, _.compact() does not modify the original array. It returns a new array with the falsy values removed.
Can _.compact() remove custom-defined falsy values?
_.compact() is designed to remove standard JavaScript falsy values only. Custom-defined falsy values require a different approach, like a custom filter function.
Is _.compact() suitable for all data types?
Yes, _.compact() works with any data type that JavaScript considers falsy. It is versatile and can handle arrays containing various data types.
Conclusion
The _.compact() method in Lodash is a simple yet powerful tool for cleaning arrays by removing falsy values. Its ability to streamline data and simplify coding makes it an invaluable function in JavaScript programming, particularly in scenarios involving data processing and manipulation. By ensuring arrays contain only meaningful elements, _.compact() significantly enhances data integrity and application performance.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.