〰️

LoDash Js

Image
https://media.geeksforgeeks.org/wp-content/cdn-uploads/20230310162729/LodashTutorial.jpg

Installation

To use Lodash in your project, you need to install it first. You can do this either via npm or by downloading the standalone library from the Lodash website.
To install Lodash via npm, open your command line interface and run the following command:
npm install lodash

Basic usage

To use Lodash in your code, you need to include it in your JavaScript file by requiring it:
const _ = require('lodash');

Arrays

Lodash provides a variety of functions for working with arrays. Here are some of the most commonly used:

_.map()

_.map(array, iteratee)
The _.map() function applies a function to each element in an array and returns a new array with the results. The first argument is the array, and the second argument is the function to apply.
const numbers = [1, 2, 3]; const squares = _.map(numbers, (n) => n * n); console.log(squares); // [1, 4, 9]

_.filter()

_.filter(array, predicate)
The _.filter() function creates a new array with all elements that pass the test implemented by the provided function. The first argument is the array, and the second argument is the function to apply.
const numbers = [1, 2, 3, 4, 5]; const evenNumbers = _.filter(numbers, (n) => n % 2 === 0); console.log(evenNumbers); // [2, 4]

_.reduce()

_.reduce(array, iteratee, [accumulator])
The _.reduce() function reduces an array to a single value by applying a function to each element of the array. The first argument is the array, the second argument is the function to apply, and the third argument (optional) is the initial value of the accumulator.
const numbers = [1, 2, 3, 4, 5]; const sum = _.reduce(numbers, (accumulator, n) => accumulator + n, 0); console.log(sum); // 15

_.orderBy()

_.orderBy(array, [iteratees=[_.identity]], [orders])
The _.orderBy() function creates a new sorted array of objects ordered by properties and orders. The first argument is the array to sort, the second argument (optional) is the property to sort by, and the third argument (optional) is the order to sort in.
const users = [ { name: 'John', age: 25 }, { name: 'Jane', age: 20 }, { name: 'Bob', age: 30 } ]; const sortedUsers = _.orderBy(users, ['name'], ['asc']); console.log(sortedUsers); // [{ name: 'Bob', age: 30 }, { name: 'Jane', age: 20 }, { name: 'John', age: 25 }]
 
Here is a list of some of the most commonly used functions in Lodash and a brief explanation of what they do:

Arrays

  • _.chunk(array, size) - Creates an array of elements split into groups the length of size.
  • _.compact(array) - Creates an array with all falsey values removed. Falsey values are false, null, 0, "", undefined, and NaN.
  • _.concat(array, [values]) - Creates a new array concatenating array with any additional arrays and/or values.
  • _.difference(array, [values]) - Creates an array of unique values not included in the other given arrays.
  • _.drop(array, [n=1]) - Creates a slice of array with n elements dropped from the beginning.
  • _.dropRight(array, [n=1]) - Creates a slice of array with n elements dropped from the end.
  • _.fill(array, value, [start=0], [end=array.length]) - Fills elements of array with value from start up to, but not including, end.
  • _.find(array, [predicate=_.identity], [fromIndex=0]) - Iterates over elements of array, returning the first element that the predicate returns truthy for.
  • _.flatten(array) - Flattens array a single level deep.
  • _.intersection([arrays]) - Creates an array of unique values that are included in all given arrays.
  • _.join(array, [separator=',']) - Converts all elements in array into a string separated by separator.
  • _.map(collection, [iteratee=_.identity]) - Creates an array of values by running each element of collection through iteratee.
  • _.orderBy(collection, [iteratees=[_.identity]], [orders]) - Sorts collection by iteratees and orders.
  • _.reduce(collection, [iteratee=_.identity], [accumulator]) - Reduces collection to a value which is the accumulated result of running each element in collection through iteratee.
  • _.reverse(array) - Reverses array.
  • _.slice(array, [start=0], [end=array.length]) - Creates a slice of array from start up to, but not including, end.
  • _.sortBy(collection, [iteratees=[_.identity]]) - Sorts collection by iteratees.

Objects

  • _.assign(object, [sources]) - Assigns own enumerable string keyed properties of source objects to the destination object.
  • _.clone(value) - Creates a shallow clone of value.
  • _.defaults(object, [sources]) - Assigns own enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to undefined.
  • _.findKey(object, [predicate=_.identity]) - Iterates over own enumerable properties of object, returning the key that the predicate returns truthy for.
  • _.get(object, path, [defaultValue]) - Gets the value at path of object. If the resolved value is undefined, the defaultValue is returned in its place.
  • _.has(object, path) - Checks if path is a direct property of object.
  • _.invert(object) - Creates an object composed of the inverted keys and values of object.
  • _.keys(object) - Creates an array of the own enumerable property names of `object
Built with Potion.so