Lodash (Javascript Library) Cheatsheet




Lodash is a Javascript library that provides utility methods for convenience, which are not by default provided with the vanilla javascript. Lodash Library is very light weight (Just 4KB gzipped) and this is the top #1 library by downloads in NPM registry

Usage

  1. In Browser Using a CDN
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

If you download lodash.js file

<script src="lodash.js"></script>
  1. Using NPM
npm i lodash

Arrays

1. chunk

Splits the given array into chunks with the chunk size provided

_.chunk(['a', 'b', 'c', 'd', 'e'], 2); // => [['a', 'b'], ['c', 'd'], ['e]]

2. compact

Removes false, null, 0, "", undefined, NaN from a given array

3. concat

creates and returns a new array by concatinating fiven array elements/ raw elements

_.concat([1, 2], 3, [4]); // => [1, 2, 3, 4]

Note: This only concatenate at 1 level of arrays, if you want to concatenate to multi level deep apply flatten too.

4. drop

dropns given number of elements from an array from array start

_.drop([1, 2, 3, 4], 2); // => [3, 4]

5. flatten

flatten given array to single level deep.

_.flatten([0, 1, [2, 3, 4], [5, [6]], 7]) // => [0, 1, 2, 3, 4, 5, [6], 7]

6. flattenDeep

Recursively flattens given array

_.flatten([0, 1, [2, 3, 4], [5, [6]], 7]) // => [0, 1, 2, 3, 4, 5, 6, 7]

Collection

1. find

return the first element that matches predicate

_.find([-1, 0, 1, 2, 3], ele => ele > 0) // => 1

Date

1. now

returns the current time in milliseconds

_.now() // => 1561104924053

Function

1. delay

Invokes a function after given wait time (in milliseconds). extra args will be passed to the function when invoked.

_.delay(func, 1000, arg1, arg2, arg3);

Lang

1. isArray

return true if given arg is an array

_.isArray([1, 2]); // => true
_.isArray(1); // => false

Math

1. mean

computes mean value of the values in given array

_.mean([5,10,85, 100]); // => 50

Number

1. random

provides a random number. lower & upper numbers are optional. If you do not provide any lower or upper, it returns a random between 0 to 1

_.random(100); // => integer between o and 100
_.random(); // => floating-point between 0 to 1
_.random(10, 20); // => integer between 10 to 20

Object

1. get

reads the value at given path

const user = {
    _id: 1,
    address: {
        streetAddress : "Foo"
    }
}
_.get(user, 'address.streetAddress'); // => Foo


┬ęCopyright 2020 OneCompiler | Privacy Policy | Terms & Conditions