Skip to content

Collection of examples with the javascript library Lodash. Lodash is a JavaScript library which provides utility functions for common programming tasks using the functional programming paradigm.

Notifications You must be signed in to change notification settings

HecFranco/Lodash-by-Samples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Commands Summary

Sources

Notes and Examples

"Util" Methods

noop

  • _.noop, This method returns undefined.
_.times(2, _.noop);   // => [undefined, undefined]

return to “Util” Methods

nthArg

  • _.nthArg([n=0]), Creates a function that gets the argument at index n. If n is negative, the nth argument from the end is returned.
var func = _.nthArg(1);
func('a', 'b', 'c', 'd');   // => 'b'
var func = _.nthArg(-2);
func('a', 'b', 'c', 'd');   // => 'c'

return to “Util” Methods


⬆ return to commands summary


“Lang” Methods

isArrayLike

  • _.isArrayLike(value), A value is considered array-like if it's not a function and has a value.length that's an integer greater than or equal to 0 and less than or equal to Number.MAX_SAFE_INTEGER.
_.isArrayLike([1, 2, 3]);               // => true
_.isArrayLike(document.body.children);  // => true
_.isArrayLike('abc');                   // => true
_.isArrayLike(_.noop);                  // => false

return to “Lang” Methods

isArrayLikeObject

  • _.isArrayLikeObject(value), This method is like _.isArrayLike except that it also checks if value is an object.
_.isArrayLikeObject([1, 2, 3]);                 // => true
_.isArrayLikeObject(document.body.children);    // => true
_.isArrayLikeObject('abc');                     // => false
_.isArrayLikeObject(_.noop);                    // => false

return to “Lang” Methods

isBoolean

  • _.isBoolean(value), Checks if value is classified as a boolean primitive or object.
_.isBoolean(false);     // => true
_.isBoolean(null);      // => false

return to “Lang” Methods

isNumber

  • _.isNumber(value), Checks if value is classified as a Number primitive or object.
_.isNumber(3);                  // => true
_.isNumber(Number.MIN_VALUE);   // => true
_.isNumber(Infinity);           // => true
_.isNumber('3');                // => false

return to “Lang” Methods

isString

  • _.isString(value), Checks if value is classified as a String primitive or object.
_.isString('abc');      // => true
_.isString(1);          // => false

return to “Lang” Methods

isDate

  • _.isDate(value), Checks if value is classified as a Date object.
_.isDate(new Date);             // => true
_.isDate('Mon April 23 2012');  // => false

return to “Lang” Methods

isEmpty

  • _.isEmpty(value), Checks if value is an empty object, collection, map, or set.
_.isEmpty(null);        // => true
_.isEmpty(true);        // => true
_.isEmpty(1);           // => true
_.isEmpty([1, 2, 3]);   // => false
_.isEmpty({ 'a': 1 });  // => false

return to “Lang” Methods

isEqual

  • _.isEqual(value, other), Performs a deep comparison between two values to determine if they are equivalent.

Note: This method supports comparing arrays, array buffers, booleans, date objects, error objects, maps, numbers, Object objects, regexes, sets, strings, symbols, and typed arrays. Object objects are compared by their own, not inherited, enumerable properties. Functions and DOM nodes are compared by strict equality, i.e. ===.

var object = { 'a': 1 };
var other = { 'a': 1 };
_.isEqual(object, other);   // => true
object === other;           // => false

return to “Lang” Methods

isEqualWith

  • _.isEqualWith(value, other, [customizer]), This method is like _.isEqual except that it accepts customizer which is invoked to compare values. If customizer returns undefined, comparisons are handled by the method instead. The customizer is invoked with up to six arguments: (objValue, othValue [, index|key, object, other, stack]).
function isGreeting(value) {
  return /^h(?:i|ello)$/.test(value);
}
function customizer(objValue, othValue) {
  if (isGreeting(objValue) && isGreeting(othValue)) {
    return true;
  }
}
var array = ['hello', 'goodbye'];
var other = ['hi', 'goodbye'];
_.isEqualWith(array, other, customizer);    // => true

return to “Lang” Methods

isFunction

  • _.isFunction(value), Checks if value is classified as a Function object.
_.isFunction(_);      // => true
_.isFunction(/abc/);  // => false

return to “Lang” Methods

isFunction

  • _.toArray(value), Converts value to an array.
_.toArray({ 'a': 1, 'b': 2 });    // => [1, 2]
_.toArray('abc');                 // => ['a', 'b', 'c']
_.toArray(1);                     // => []
_.toArray(null);                  // => []

return to “Lang” Methods

gte

  • _.gte(value, other), Checks if value is greater than or equal to other.
_.gte(3, 1);    // => true
_.gte(3, 3);    // => true
_.gte(1, 3);    // => false

return to “Lang” Methods

lte

  • _.lte(value, other), Checks if value is less than or equal to other.
_.lte(1, 3);    // => true
_.lte(3, 3);    // => true
_.lte(3, 1);    // => false

return to “Lang” Methods

clone

  • _.clone(value), Creates a shallow clone of value.

Note: This method is loosely based on the structured clone algorithm and supports cloning arrays, array buffers, booleans, date objects, maps, numbers, Object objects, regexes, sets, strings, symbols, and typed arrays. The own enumerable properties of arguments objects are cloned as plain objects. An empty object is returned for uncloneable values such as error objects, functions, DOM nodes, and WeakMaps.

var objects = [{ 'a': 1 }, { 'b': 2 }];
var shallow = _.clone(objects);
console.log(shallow[0] === objects[0]);   // => true

return to “Lang” Methods

cloneDeep

  • _.cloneDeep(value), This method is like _.clone except that it recursively clones value.
var objects = [{ 'a': 1 }, { 'b': 2 }];
var deep = _.cloneDeep(objects);
console.log(deep[0] === objects[0]);    // => false

return to “Lang” Methods


⬆ return to commands summary


“String” Methods

camelCase

  • _.camelCase([string='']), Converts string to camel case.
_.camelCase('Foo Bar');       // => 'fooBar'
_.camelCase('--foo-bar--');   // => 'fooBar'
_.camelCase('__FOO_BAR__');   // => 'fooBar'

return to “String” Methods

capitalize

  • _.capitalize([string='']), Converts the first character of string to upper case and the remaining to lower case.
_.capitalize('FRED');   // => 'Fred'

return to “String” Methods

deburr

  • _.deburr([string='']), Deburrs string by converting Latin-1 Supplement and Latin Extended-A letters to basic Latin letters and removing combining diacritical marks.
_.deburr('déjà vu');  // => 'deja vu'

return to “String” Methods

kebabCase

  • _.kebabCase([string='']), Converts string to kebab case.
_.kebabCase('Foo Bar');     // => 'foo-bar'
_.kebabCase('fooBar');      // => 'foo-bar'
_.kebabCase('__FOO_BAR__'); // => 'foo-bar'

return to “String” Methods

pad

  • _.pad([string=''], [length=0], [chars=' ']), Pads string on the left and right sides if it's shorter than length. Padding characters are truncated if they can't be evenly divided by length.

arguments

  1. [string=''] (string): The string to pad.
  2. [length=0] (number): The padding length.
  3. [chars=' '] (string): The string used as padding.
_.pad('abc', 8);        // => '  abc   '
_.pad('abc', 8, '_-');  // => '_-abc_-_'
_.pad('abc', 3);        // => 'abc'

return to “String” Methods

words

  • _.words([string=''], [pattern]), Splits string into an array of its words.

arguments

  1. [string=''] (string): The string to inspect.
  2. [pattern] (RegExp|string): The pattern to match words.
_.words('fred, barney, & pebbles');               // => ['fred', 'barney', 'pebbles']
_.words('fred, barney, & pebbles', /[^, ]+/g);    // => ['fred', 'barney', '&', 'pebbles']

return to “String” Methods


⬆ return to commands summary


“Array” Methods

chunk

  • _.chunk(array, [size=1]), Creates an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.

arguments

  1. array (Array): The array to process.
  2. [size=1] (number): The length of each chunk
_.chunk(['a', 'b', 'c', 'd'], 2);   // => [['a', 'b'], ['c', 'd']]
_.chunk(['a', 'b', 'c', 'd'], 3);   // => [['a', 'b', 'c'], ['d']]

return to “Array” Methods

difference

  • _.difference(array, [values]), Creates an array of array values not included in the other given arrays using SameValueZero for equality comparisons. The order and references of result values are determined by the first array.
_.difference([2, 1], [2, 3]);   // => [1]

return to “Array” Methods

pull

  • _.pull(array, [values]), Removes all given values from array using SameValueZero for equality comparisons.

Note: Unlike _.without, this method mutates array. Use _.remove to remove elements from an array by predicate.

var array = ['a', 'b', 'c', 'a', 'b', 'c'];
_.pull(array, 'a', 'c');
console.log(array);   // => ['b', 'b']

return to “Array” Methods

pullAll

  • _.pullAll(array, values), This method is like _.pull except that it accepts an array of values to remove.
var array = ['a', 'b', 'c', 'a', 'b', 'c'];
_.pullAll(array, ['a', 'c']);
console.log(array);   // => ['b', 'b']

return to “Array” Methods

differenceBy

  • _.differenceBy(array, [values], [iteratee=_.identity]), This method is like _.difference except that it accepts iteratee which is invoked for each element of array and values to generate the criterion by which they're compared. The order and references of result values are determined by the first array. The iteratee is invoked with one argument:(value).

Arguments

  1. array (Array): The array to inspect.
  2. [values] (...Array): The values to exclude.
  3. [iteratee=_.identity] (Function): The iteratee invoked per element.
_.differenceBy([2.1, 1.2], [2.3, 3.4], Math.floor);             // => [1.2]
// The `_.property` iteratee shorthand.
_.differenceBy([{ 'x': 2 }, { 'x': 1 }], [{ 'x': 1 }], 'x');    // => [{ 'x': 2 }]
_.differenceBy([-1,-2,-3,-4,-5],[1,3,4], Math.abs)              // =>[-2,-5]
const animalCollection1 = [ 
    { type: 'dog',      name: 'Mishka' }, 
    { type: 'hamster',  name: 'Jasper' }, 
    { type: 'cat',      name: 'Kota' }, 
]; 
const animalCollection2 = [ 
    { type: 'dog',      name: 'Toby' }, 
    { type: 'cat',      name: 'Luna' }, 
    { type: 'fish',     name: 'Bob' }, 
];
_.differenceBy(
  animalCollection1, 
  animalCollection2, 
  (animal) => animal.type);  // => 0: {type: "hamster", name: "Jasper"}
const animalCollection1 = [ 
    { type: 'dog',      name: 'Mishka' }, 
    { type: 'hamster',  name: 'Jasper' }, 
    { type: 'cat',      name: 'Kota' }, 
]; 
const animalCollection2 = [ 
    { type: 'dog',      name: 'Toby' }, 
    { type: 'cat',      name: 'Luna' }, 
    { type: 'fish',     name: 'Bob' }, 
];
const func = (animal)=>{
    return animal.type;
};
_.differenceBy(animalCollection1, animalCollection2, func);   // => 0: {type: "hamster", name: "Jasper"}

return to “Array” Methods

pullAllBy

  • _.pullAllBy(array, values, [iteratee=_.identity]), This method is like _.pullAll except that it accepts iteratee which is invoked for each element of array and values to generate the criterion by which they're compared. The iteratee is invoked with one argument: (value).

arguments

  1. array (Array): The array to modify.
  2. values (Array): The values to remove.
  3. [iteratee=_.identity] (Function): The iteratee invoked per element.
var array = [{ 'x': 1 }, { 'x': 2 }, { 'x': 3 }, { 'x': 1 }];
_.pullAllBy(array, [{ 'x': 1 }, { 'x': 3 }], 'x');
console.log(array);   // => [{ 'x': 2 }]
const animalCollection1 = [ 
    { type: 'dog',      name: 'Mishka' }, 
    { type: 'hamster',  name: 'Jasper' }, 
    { type: 'cat',      name: 'Kota' }, 
]; 
const animalCollection2 = [ 
    { type: 'dog',      name: 'Toby' }, 
    { type: 'cat',      name: 'Luna' }, 
    { type: 'fish',     name: 'Bob' }, 
];
_.pullAllBy(
  animalCollection1, 
  animalCollection2, 
  (animal) => animal.type);  
console.log(animalCollection1); // => 0: {type: "hamster", name: "Jasper"}
const animalCollection1 = [ 
    { type: 'dog',      name: 'Mishka' }, 
    { type: 'hamster',  name: 'Jasper' }, 
    { type: 'cat',      name: 'Kota' }, 
]; 
const animalCollection2 = [ 
    { type: 'dog',      name: 'Toby' }, 
    { type: 'cat',      name: 'Luna' }, 
    { type: 'fish',     name: 'Bob' }, 
];
const func = (animal)=>{
    return animal.type;
};
_.pullAllBy(animalCollection1, animalCollection2, func);
console.log(animalCollection1); // => 0: {type: "hamster", name: "Jasper"}

return to “Array” Methods

differenceWith

  • _.differenceWith(array, [values], [comparator]), This method is like _.difference except that it accepts comparator which is invoked to compare elements of array to values. The order and references of result values are determined by the first array. The comparator is invoked with two arguments: (arrVal, othVal).

arguments

  1. array (Array): The array to inspect.
  2. [values] (...Array): The values to exclude.
  3. [comparator (Function): The comparator invoked per element.
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
_.differenceWith(objects, [{ 'x': 1, 'y': 2 }], _.isEqual);   // => [{ 'x': 2, 'y': 1 }]
const animalCollection1 = [ 
    { type: 'dog',      name: 'Mishka' }, 
    { type: 'hamster',  name: 'Jasper' }, 
    { type: 'cat',      name: 'Kota' }, 
]; 
const animalCollection2 = [ 
    { type: 'dog',      name: 'Toby' }, 
    { type: 'cat',      name: 'Luna' }, 
    { type: 'fish',     name: 'Bob' }, 
];
const funcwith = (animalCollection1, animalCollection2)=>{
    return animalCollection1.type === animalCollection2.type;
};
_.differenceWith(animalCollection1, animalCollection2, funcwith);   // => {type: "hamster", name: "Jasper"}

return to “Array” Methods

pullAllWith

  • _.pullAllWith(array, values, [comparator]), This method is like _.pullAll except that it accepts comparator which is invoked to compare elements of array to values. The comparator is invoked with two arguments: (arrVal, othVal).

arguments

  1. array (Array): The array to modify.
  2. values (Array): The values to remove.
  3. [comparator] (Function): The comparator invoked per element.
var array = [{ 'x': 1, 'y': 2 }, { 'x': 3, 'y': 4 }, { 'x': 5, 'y': 6 }];
_.pullAllWith(array, [{ 'x': 3, 'y': 4 }], _.isEqual);
console.log(array);     // => [{ 'x': 1, 'y': 2 }, { 'x': 5, 'y': 6 }]
const animalCollection1 = [ 
    { type: 'dog',      name: 'Mishka' }, 
    { type: 'hamster',  name: 'Jasper' }, 
    { type: 'cat',      name: 'Kota' }, 
]; 
const animalCollection2 = [ 
    { type: 'dog',      name: 'Toby' }, 
    { type: 'cat',      name: 'Luna' }, 
    { type: 'fish',     name: 'Bob' }, 
];
const funcwith = (animalCollection1, animalCollection2)=>{
    return animalCollection1.type === animalCollection2.type;
};
_.pullAllWith(animalCollection1, animalCollection2, funcwith);
console.log(animalCollection1);     // => {type: "hamster", name: "Jasper"}

return to “Array” Methods

union

  • _.union([arrays]), Creates an array of unique values, in order, from all given arrays using SameValueZero for equality comparisons.
_.union([2], [1, 2]);   // => [2, 1]
const coordinatesCollection1 = [ 
    { x: 0, y: 3 }, 
    { x: 2, y: 5 }, 
    { x: 2, y: 4 },
];
const coordinatesCollection2 = [ 
    { x: 0, y: 7 }, 
    { x: 1, y: 8 }, 
    { x: 2, y: 10 }, 
];
_.union(coordinatesCollection1, coordinatesCollection2, 'x');   
// => [{x: 0, y: 3}, {x: 2, y: 5}, {x: 1, y: 8}, { x: 0, y: 7 }, { x: 1, y: 8 }, { x: 2, y: 10 } ]

return to “Array” Methods

unionBy

  • _.unionBy([arrays], [iteratee=_.identity]), This method is like _.union except that it accepts iteratee which is invoked for each element of each arrays to generate the criterion by which uniqueness is computed. Result values are chosen from the first array in which the value occurs. The iteratee is invoked with one argument: (value).

arguments

  1. [arrays] (...Array): The arrays to inspect.
  2. [iteratee=_.identity](Function): The iteratee invoked per element.
_.unionBy([2.1], [1.2, 2.3], Math.floor);                 // => [2.1, 1.2]
// The `_.property` iteratee shorthand.
_.unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');   // => [{ 'x': 1 }, { 'x': 2 }]
const coordinatesCollection1 = [ 
    { x: 0, y: 3 }, 
    { x: 2, y: 5 }, 
    { x: 2, y: 4 },
];
const coordinatesCollection2 = [ 
    { x: 0, y: 7 }, 
    { x: 1, y: 8 }, 
    { x: 2, y: 10 }, 
    { x: 2, y: 4 }, 
];
_.unionBy(coordinatesCollection1, coordinatesCollection2, 'x');   // => [{x: 0, y: 3}, {x: 2, y: 5}, {x: 1, y: 8}]

return to “Array” Methods

unionWith

  • _.unionWith([arrays], [comparator]), This method is like _.union except that it accepts comparator which is invoked to compare elements of arrays. Result values are chosen from the first array in which the value occurs. The comparator is invoked with two arguments: (arrVal, othVal).

arguments

  1. [arrays] (...Array): The arrays to inspect.
  2. [comparator] (Function): The comparator invoked per element.
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
_.unionWith(objects, others, _.isEqual);    // => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
const coordinatesCollection1 = [ 
    { x: 0, y: 3 }, 
    { x: 2, y: 5 }, 
    { x: 2, y: 4 },
];
const coordinatesCollection2 = [ 
    { x: 0, y: 7 }, 
    { x: 1, y: 8 }, 
    { x: 2, y: 10 }, 
    { x: 2, y: 4 },
];
_.unionWith(coordinatesCollection1, coordinatesCollection2, 'x');   
// => [{x: 0, y: 3}, {x: 2, y: 5}, {x: 2, y: 4}, {x: 0, y: 7}, {x: 1, y: 8}, {x: 2, y: 10}, {x: 2, y: 4}]

return to “Array” Methods

intersectionBy

  • _.intersectionBy([arrays], [iteratee=_.identity]), This method is like _.intersection except that it accepts iteratee which is invoked for each element of each arrays to generate the criterion by which they're compared. The order and references of result values are determined by the first array. The iteratee is invoked with one argument: (value).

arguments

  1. [arrays] (...Array): The arrays to inspect.
  2. [iteratee=_.identity] (Function): The iteratee invoked per element.
_.intersectionBy([2.1, 1.2], [2.3, 3.4], Math.floor);             // => [2.1]
// The `_.property` iteratee shorthand.
_.intersectionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');    // => [{ 'x': 1 }]
const coordinatesCollection1 = [ 
    { x: 0, y: 3 }, 
    { x: 2, y: 5 }, 
    { x: 2, y: 4 },
];
const coordinatesCollection2 = [ 
    { x: 0, y: 7 }, 
    { x: 1, y: 8 }, 
    { x: 2, y: 10 }, 
    { x: 2, y: 4 },
];
_.intersectionBy(coordinatesCollection1, coordinatesCollection2, 'x');  // => [{x: 0, y: 3}, {x: 2, y: 5}]

return to “Array” Methods

intersectionWith

  • _.intersectionWith([arrays], [comparator]), This method is like _.intersection except that it accepts comparator which is invoked to compare elements of arrays. The order and references of result values are determined by the first array. The comparator is invoked with two arguments: (arrVal, othVal).

arguments

  1. [arrays] (...Array): The arrays to inspect.
  2. [comparator] (Function): The comparator invoked per element.
var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];
_.intersectionWith(objects, others, _.isEqual);   // => [{ 'x': 1, 'y': 2 }]
const coordinatesCollection1 = [ 
    { x: 0, y: 7, z:2 },
    { x: 2, y: 5, z:1 },
    { x: 1, y: 1, z: 1}, 
];
const coordinatesCollection2 = [ 
    { x: 0, y: 7, z: 3 }, 
    { x: 1, y: 8, z: 1 }, 
    { x: 2, y: 10, z: 1 }, 
]; 
const func = ({x1, z1}, {x2, z2})=>{ 
    return x1 == x2 && z1 == z2 ;
} 
_.intersectionWith(coordinatesCollection1, coordinatesCollection2, func); // => {x: 0, y: 7, z: 2}

return to “Array” Methods


⬆ return to commands summary


"Colletion" Methods

countBy

return to "Colletion" Methods

orderBy

return to "Colletion" Methods

filter

return to "Colletion" Methods

map

return to "Colletion" Methods

sample

return to "Colletion" Methods

sampleBy

return to "Colletion" Methods

shuffle

return to "Colletion" Methods

defaults

return to "Colletion" Methods

forIn

return to "Colletion" Methods

forOwn

return to "Colletion" Methods

ary

return to "Colletion" Methods

map

return to "Colletion" Methods

unary

return to "Colletion" Methods

curry

return to "Colletion" Methods

curryRight

return to "Colletion" Methods

partial

return to "Colletion" Methods

memorize

return to "Colletion" Methods


⬆ return to commands summary


About

Collection of examples with the javascript library Lodash. Lodash is a JavaScript library which provides utility functions for common programming tasks using the functional programming paradigm.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published