A RetroSearch Logo

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

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/lodash-_-flatten-method/ below:

Lodash _.flatten() Method - GeeksforGeeks

Lodash _.flatten() Method

Last Updated : 02 Sep, 2024

Lodash _.flatten() method is used to flatten the array to one level deep and it returns a new flattened array.

Syntax:
_.flatten( array );
Parameter: Return Value:

Example 1: In this example, er are flattening the array to a single level by the use of the _.flatten() method.

JavaScript
// Requiring the lodash library
const _ = require("lodash");

// Original array
let array1 = [[1, 2], [4, 5], [7, 8]]

// Using _.flatten() method
let newArray = _.flatten(array1);

// Printing original Array
console.log("original Array1: ", array1)

// Printing the newArray
console.log("new Array: ", newArray)

Output:

Example 2: In this example, er are flattening the array of object to a single level by the use of the _.flatten() method.

JavaScript
// Requiring the lodash library
const _ = require("lodash");

// Original array
let array1 = [[{ "a": 1 }],
[{ "b": 2 }, { "c": 3 }]]

// Using _.flatten() method
let newArray = _.flatten(array1);

// Printing original Array
console.log("original Array1: ", array1)

// Printing the newArray
console.log("new Array: ", newArray)

Output: 

Example 3: In this example, er are flattening the empty array to a single level by the use of the _.flatten() method.

JavaScript
// Requiring the lodash library
const _ = require("lodash");

// Original array
let array1 = [[], [[[]]], [[]], []]

// Using _.flatten() method
let newArray = _.flatten(array1);

// Printing original Array
console.log("original Array1: ", array1)

// Printing the newArray
console.log("new Array: ", 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