A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-compact-function/ below:

Lodash _.compact() Function - GeeksforGeeks

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:

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

Return Value: 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.

javascript
// Requiring the lodash library 
let _ = require("lodash"); 
  
// Original array to be compacted 
let array = [0, 1, false, 2, '', 3]; 
  
let newArray = _.compact(array); 
console.log("Before compact: " + array); 
  
// Printing newArray  
console.log("After compact: " + newArray); 

Output:

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

javascript
// Requiring the lodash library 
let _ = require("lodash"); 
  
// Original array to be compacted 
let array = [0, false, '', undefined, NaN]; 
  
let newArray = _.compact(array); 
console.log("Before compact: " + array); 
  
// Printing newArray  
console.log("After compact: " + newArray); 

Output:

output

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

javascript
// Requiring the lodash library 
let _ = require("lodash");

// Original array to be compacted 
let array = [false, 'HTML', NaN,
    'CSS', 'undefined'];

let newArray = _.compact(array);
console.log("Before compact: " + array);

// Printing newArray  
console.log("After compact: " + newArray); 

Output:

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

javascript
// Requiring the lodash library 
let _ = require("lodash"); 
  
// Original array to be compacted 
let array = [false, true, 'yes', 'no', "no2"]; 
  
let newArray = _.compact(array); 
console.log("Before compact: " + array); 
  
// Printing newArray  
console.log("After compact: " + newArray); 

Output:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4