Skip to content

WebDevHubs

  • Home
  • HTML
  • CSS
  • JavaScript
  • Web Technologies
  • Web Templates
  • Toggle search form

Lodash _.compact() Method

Posted on March 4, 2025March 4, 2025 By Admin No Comments on Lodash _.compact() Method

The _.compact() method is used to create a new array by removing all falsy values from the given array. In JavaScript, the falsy values are – false, null, 0, "" (empty string), undefined, and NaN.

Syntax

_.compact(array)

Parameters

  • array (Required): The input array from which falsy values will be filtered out. It can be any valid JavaScript array containing elements of any data type (strings, numbers, objects, etc.).

Return Value

It returns a new array after removing all falsy values from the original array. It preserves the order of the truthy values. If the input array contains only falsy values, the resulting array will be empty.

Lodash _.compact() Method Code Examples

1. Removing Falsy Values from an Array of Numbers

In this example, we remove falsy values from an array containing both truthy and falsy values.

const _ = require('lodash');

const arr = [0, 1, false, 2, '', 3, null, 4, NaN, 5];
const compactArr = _.compact(arr);

console.log(compactArr);
// Output: [1, 2, 3, 4, 5]

In this example, all the falsy values (0, false, '', null, and NaN) are removed, and only the truthy values remain.

2. Compacting an Array with Strings

Here, we filter out falsy values from an array containing strings.

const arr = ['hello', '', 'world', '!', null, 'JavaScript'];
const compactArr = _.compact(arr);

console.log(compactArr);
// Output: ['hello', 'world', '!', 'JavaScript']

In this case, the empty string ("") and null are removed from the array, leaving only the non-falsy values.

3. Compacting an Array with undefined and false

This example demonstrates how _.compact() removes both undefined and false.

const array = [undefined, false, true, 'truthy', 0, 1];
const compactedArray = _.compact(array);

console.log(compactedArray);
// Output: [true, 'truthy', 1]

The undefined, false, and 0 values are filtered out, and only the truthy values (true, 'truthy', and 1) remain in the array.

4. Compacting an Array with Only Falsy Values

const array = [false, null, '', NaN, undefined, 0];
const compactedArray = _.compact(array);

console.log(compactedArray);
// Output: []

Since all elements are falsy, the output is an empty array.

5. Compacting an Array with Objects and Arrays

The _.compact() method also works with arrays containing objects and arrays, as long as the values themselves are truthy.

const array = [1, {}, [], false, null, [1, 2], undefined];
const compactedArray = _.compact(array);

console.log(compactedArray);
// Output: [1, {}, [], [1, 2]]

Here, the empty array ([]), the empty object ({}), and the array [1, 2] are truthy, so they remain in the result. Only the false, null, and undefined values are removed.

6. Compacting an Array with Mixed Data Types

This example shows that _.compact() can filter out falsy values from arrays containing different data types.

const array = [false, 42, 'hello', NaN, 0, {name: 'John'}, undefined];
const compactedArray = _.compact(array);

console.log(compactedArray);
// Output: [42, 'hello', {name: 'John'}]

As expected, the falsy values (false, NaN, 0, and undefined) are removed, leaving the truthy values.

FAQs on _.compact() Method

1. What are falsy values in JavaScript?

In JavaScript, falsy values are considered false when evaluated in a boolean context. These values include:

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

Anything that is not a falsy value is considered a truthy value.

2. Does _.compact() method modify the original array?

No, _.compact() does not modify the original array. It creates and returns a new array after removing the falsy values.

3. Can I use _.compact() with arrays of objects?

Yes, _.compact() method can also handle arrays containing objects. If the objects themselves are truthy (i.e., they are not null or undefined), it will be retained in the resulting array. Objects and arrays are considered truthy values in JavaScript.

4. How does _.compact() handle empty strings?

Empty strings ("") are falsy values in JavaScript. Therefore, _.compact() will remove empty strings from the array.

5. What happens if I pass an empty array to _.compact()?

If you pass an empty array to _.compact(), the result will also be an empty array because there are no elements to remove.

const array = [];
const compactedArray = _.compact(array);

console.log(compactedArray);
// Output: []

6. Can I use _.compact() with non-array values?

No, the _.compact() method only works with arrays. If you pass a non-array value (like an object, string, or number), Lodash will throw an error. Ensure the input is an array before using this method.

7. What is the difference between _.compact() and _.filter()?

While both methods remove unwanted values from arrays, they have different purposes:

  • _.compact() is specifically designed to remove falsy values.
  • _.filter() can be used for more general filtering by providing a predicate function. It allows you to filter based on any condition, not just falsy values.

8. Does _.compact() preserve the array’s order?

Yes, _.compact() preserves the order of the original array. The truthy values will appear in the same order in the resulting array as they did in the original array.

JavaScript, Lodash, Web Technologies Tags:JavaScript-Lodash, Lodash-Array, Lodash-Method

Post navigation

Previous Post: Lodash _.chunk() Method
Next Post: Create a Moving Car Animation Using HTML and CSS

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archives

  • June 2025
  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • JavaScript
  • Lodash
  • PHP
  • Python
  • Web Technologies
  • Web Templates

Recent Posts

  • JavaScript Array isArray() Method
  • JavaScript Array forEach() Method
  • JavaScript Array includes() Method
  • JavaScript Array keys() Method
  • JavaScript Array lastIndexOf() Method

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme