Lodash is a javascript library utility that makes it easy to work with arrays, numbers, objects, and other javascript primitives. A Lodash function is one of those functions that perform a single operation on our data, like sorting our array or flattening the array.
Here, we'll look at how to import a single Lodash function:
const { functionName } = require("lodash");
A Lodash function may take a single parameter, a list of parameters, and even a function. The sample parameters of this function are as follows:
const { functionName } = require("lodash");// single parameterfunctionName(params);// multiple parametersfunctionName(param1, param2);// and even a parameter with a functionfunctionName(param, doSomething());
In the code below, we'll import the flattenDeep
function from the Lodash to flatten our complex array completely.
const { flattenDeep } = require("./lodash.js");const myArray = [[1,2,3],4, 5, 6,[[7], [8], [9]]];const myFlatArray = flattenDeep(myArray);console.log(myFlatArray);
Here is the line-by-line explanation of the code above:
flattenDeep
function from the Lodash to flatten our array. This function takes a single parameter, an array that needs to be flattened.flattenDeep
function on the array to make our array 1-dimensional.