VOOZH about

URL: https://www.geeksforgeeks.org/javascript/lodash-_-compact-function/

⇱ Lodash _.compact() Function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Lodash _.compact() Function

Last Updated : 30 Aug, 2024

Lodash _.compact() function is used to create an array with all false values removed in JavaScript. so it returns a new array of filtered values.

Syntax:

_.compact(array);

Parameters:

  • array: It is an array to be compacted.

Note: The values are false, null, 0, "", undefined, and NaN are falsey.

Return Value:

  • This function returns the array after filtering the values.

Examples of Lodash _.compact() Function

Example 1: In this example, we are passing a list of both the true and the false elements to the _.compact() function.

Output:

👁 Image

Example 2: In this example, we are passing a list containing all the false values to the _.compact() function.

Output:



👁 Screenshot-2023-10-18-122431
output

Example 3: In this example, we are passing a list which contains a false element into _.compact() function.

Output:

👁 Image

Example 4: In this example, we are passing a list containing modified false values to the _.compact() function.

Output:

👁 Image
Comment