KEMBAR78
Lodash js | PDF
Lodash js
Jason
lodash js
Array
Chain
Collection
Function
Lang
Math
Number
Object
String
Utility
Methods
Properties
_(value)
.Creates a lodash object which wraps value to enable implicit chaining.
.Methods that operate on and return arrays, collections, and functions can be
chained together.
.Methods that return a boolean or single value will automatically end the chain
returning the unwrapped value
.The chainable wrapper methods are : filter, sort, sortBy...etc
.The wrapper methods that are not chainable by default are : first, sum, trim...etc
_(value)
var wrapped = _([1, 2, 3]);
// returns an unwrapped value
wrapped.reduce(function(sum, n) {
return sum + n;
});
// → 6
// returns a wrapped value
var squares = wrapped.map(function(n) {
return n * n;
});
_.isArray(squares);
// → false
_.isArray(squares.value());
// → true
_.chain(value)
.Creates a lodash object that wraps value with explicit method chaining
enabled.
.Arguments value (*): The value to wrap.
.Returns:(Object): Returns the new lodash wrapper instance.
_.chain(value)
var users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'pebbles', 'age': 1 }
];
var youngest = _.chain(users)
.sortBy('age')
.map(function(chr) {
return chr.user + ' is ' + chr.age;
})
.first()
.value();
// → 'pebbles is 1'
_.tap
.This method invokes interceptor
and returns value.
. _tap(value, interceptor, [thisArg])
.Returns:(*): Returns value.
_([1, 2, 3]).tap(function(array) {
array.pop();
})
.reverse()
.value();
// → [2, 1]
_.thru
.This method is like _.tap except
that it returns the result of interceptor.
. _thru(value, interceptor, [thisArg])
.Returns:(*): Returns the result of interceptor
_(' abc ')
.chain()
.trim()
.thru(function(value) {
return [value];
})
.value();
// → ['abc']
_.flatten
.Flattens a nested array.
. _flatten(array, [isDeep])
.Returns:(Array): Returns the new flattened array.
_.flatten([1, [2, 3, [4]]]);
// → [1, 2, 3, [4]]
// using isDeep
_.flatten([1, [2, 3, [4]]], true);
// → [1, 2, 3, 4]
_intersection
.Creates an array of unique values in
all provided arrays.
. _intersection(array)
.Returns:(Array): Returns the new array of shared
values.
_.intersection([1, 2], [4, 2], [2, 1]);
// → [2]
_.xor
.Creates an array that is the symmetric difference
of the provided arrays.
. _.xor(array)
.Returns:(Array): Returns the new array of values.
_.xor([1, 2], [4, 2]);
// → [1, 4]
_.pluck
.Gets the value of key from all elements
in collection.
._.pluck(collection, key)
.Returns:(Array): Returns the property values
var users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 }
];
_.pluck(users, 'user');
// → ['barney', 'fred']
_where
.Performs a deep comparison between each element in collection and the source object,
returning an array of all elements that have equivalent property values.
._.where(collection, source)
.Returns:(Array): Returns the new filtered array.
var users = [
{ 'user': 'barney', 'age': 36, 'active': false, 'pets': ['hoppy'] },
{ 'user': 'fred', 'age': 40, 'active': true, 'pets': ['baby puss', 'dino'] }
];
_.pluck(_.where(users, { 'age': 36, 'active': false }), 'user');
// → ['barney']
_.uniqueId
.Generates a unique ID. If prefix is provided the ID
is appended to it.
._.uniqueId([prefix])
.Returns:(string): Returns the unique ID.
_.uniqueId('contact_');
// → 'contact_104'
_.uniqueId();
// → '105'
_.curry
.Creates a function that accepts one or more
arguments of func that when called either invokes
func returning its result
._.curry(func, [arity=func.length])
.Returns:(Function): Returns the new curried function.
var abc = function(a, b, c) {
return [a, b, c];
};
var curried = _.curry(abc);
curried(1)(2)(3);
// → [1, 2, 3]
curried(1, 2)(3);
// → [1, 2, 3]
curried(1)(_, 3)(2);
// → [1, 2, 3]
_.bind
.Creates a function that invokes func with the this binding of
thisArg and prepends any additional _.bind arguments to
those provided to the bound function.
._.bind(func, thisArg, [partials])
.Returns:(Function): Returns the new bound function.
var greet = function(greeting, punctuation) {
return greeting + ' ' + this.user + punctuation;
};
var object = { 'user': 'fred' };
var bound = _.bind(greet, object, 'hi');
bound('!');
// → 'hi fred!'
// using placeholders
var bound = _.bind(greet, object, _, '!');
bound('hi');
// → 'hi fred!'
_.bindAll
.Binds methods of an object to the object itself, overwriting
the existing method. Method names may be specified as
individual arguments or as arrays of method names.
._.bindAll(object, [methodNames])
.Returns:(Object): Returns object.
var view = {
'label': 'docs',
'onClick': function() {
console.log('clicked ' + this.label);
}
};
_.bindAll(view);
$('#docs').on('click', view.onClick);
// → logs 'clicked docs' when the element
is clicked
_.get
.Gets the property value of path on object. If the resolved
value is undefined the defaultValue is used in its place.
._.get(object, path, [defaultValue])
.Returns:Returns the resolved Value.
var object = { 'a': [{ 'b': { 'c': 3 } }] };
_.get(object, 'a[0].b.c');
// → 3
_.get(object, ['a', '0', 'b', 'c']);
// → 3
_.get(object, 'a.b.c', 'default');
// → 'default'
_template
var compiled = _.template('hello <%= user %>!');
compiled({ 'user': 'fred' });
// → 'hello fred!'
var compiled = _.template('<b><%- value %></b>');
compiled({ 'value': '<script>' });
// → '<b>&lt;script&gt;</b>'
var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
compiled({ 'users': ['fred', 'barney'] });
// → '<li>fred</li><li>barney</li>'
var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
var compiled = _.template(text, { 'imports': { 'jq': jQuery } });
compiled({ 'users': ['fred', 'barney'] });
// → '<li>fred</li><li>barney</li>'
Reference
API Documentaion : https://lodash.com/docs
DevDocs: http://devdocs.io/lodash/

Lodash js

  • 1.
  • 2.
  • 3.
    _(value) .Creates a lodashobject which wraps value to enable implicit chaining. .Methods that operate on and return arrays, collections, and functions can be chained together. .Methods that return a boolean or single value will automatically end the chain returning the unwrapped value .The chainable wrapper methods are : filter, sort, sortBy...etc .The wrapper methods that are not chainable by default are : first, sum, trim...etc
  • 4.
    _(value) var wrapped =_([1, 2, 3]); // returns an unwrapped value wrapped.reduce(function(sum, n) { return sum + n; }); // → 6 // returns a wrapped value var squares = wrapped.map(function(n) { return n * n; }); _.isArray(squares); // → false _.isArray(squares.value()); // → true
  • 5.
    _.chain(value) .Creates a lodashobject that wraps value with explicit method chaining enabled. .Arguments value (*): The value to wrap. .Returns:(Object): Returns the new lodash wrapper instance.
  • 6.
    _.chain(value) var users =[ { 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }, { 'user': 'pebbles', 'age': 1 } ]; var youngest = _.chain(users) .sortBy('age') .map(function(chr) { return chr.user + ' is ' + chr.age; }) .first() .value(); // → 'pebbles is 1'
  • 7.
    _.tap .This method invokesinterceptor and returns value. . _tap(value, interceptor, [thisArg]) .Returns:(*): Returns value. _([1, 2, 3]).tap(function(array) { array.pop(); }) .reverse() .value(); // → [2, 1]
  • 8.
    _.thru .This method islike _.tap except that it returns the result of interceptor. . _thru(value, interceptor, [thisArg]) .Returns:(*): Returns the result of interceptor _(' abc ') .chain() .trim() .thru(function(value) { return [value]; }) .value(); // → ['abc']
  • 9.
    _.flatten .Flattens a nestedarray. . _flatten(array, [isDeep]) .Returns:(Array): Returns the new flattened array. _.flatten([1, [2, 3, [4]]]); // → [1, 2, 3, [4]] // using isDeep _.flatten([1, [2, 3, [4]]], true); // → [1, 2, 3, 4]
  • 10.
    _intersection .Creates an arrayof unique values in all provided arrays. . _intersection(array) .Returns:(Array): Returns the new array of shared values. _.intersection([1, 2], [4, 2], [2, 1]); // → [2]
  • 11.
    _.xor .Creates an arraythat is the symmetric difference of the provided arrays. . _.xor(array) .Returns:(Array): Returns the new array of values. _.xor([1, 2], [4, 2]); // → [1, 4]
  • 12.
    _.pluck .Gets the valueof key from all elements in collection. ._.pluck(collection, key) .Returns:(Array): Returns the property values var users = [ { 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 } ]; _.pluck(users, 'user'); // → ['barney', 'fred']
  • 13.
    _where .Performs a deepcomparison between each element in collection and the source object, returning an array of all elements that have equivalent property values. ._.where(collection, source) .Returns:(Array): Returns the new filtered array. var users = [ { 'user': 'barney', 'age': 36, 'active': false, 'pets': ['hoppy'] }, { 'user': 'fred', 'age': 40, 'active': true, 'pets': ['baby puss', 'dino'] } ]; _.pluck(_.where(users, { 'age': 36, 'active': false }), 'user'); // → ['barney']
  • 14.
    _.uniqueId .Generates a uniqueID. If prefix is provided the ID is appended to it. ._.uniqueId([prefix]) .Returns:(string): Returns the unique ID. _.uniqueId('contact_'); // → 'contact_104' _.uniqueId(); // → '105'
  • 15.
    _.curry .Creates a functionthat accepts one or more arguments of func that when called either invokes func returning its result ._.curry(func, [arity=func.length]) .Returns:(Function): Returns the new curried function. var abc = function(a, b, c) { return [a, b, c]; }; var curried = _.curry(abc); curried(1)(2)(3); // → [1, 2, 3] curried(1, 2)(3); // → [1, 2, 3] curried(1)(_, 3)(2); // → [1, 2, 3]
  • 16.
    _.bind .Creates a functionthat invokes func with the this binding of thisArg and prepends any additional _.bind arguments to those provided to the bound function. ._.bind(func, thisArg, [partials]) .Returns:(Function): Returns the new bound function. var greet = function(greeting, punctuation) { return greeting + ' ' + this.user + punctuation; }; var object = { 'user': 'fred' }; var bound = _.bind(greet, object, 'hi'); bound('!'); // → 'hi fred!' // using placeholders var bound = _.bind(greet, object, _, '!'); bound('hi'); // → 'hi fred!'
  • 17.
    _.bindAll .Binds methods ofan object to the object itself, overwriting the existing method. Method names may be specified as individual arguments or as arrays of method names. ._.bindAll(object, [methodNames]) .Returns:(Object): Returns object. var view = { 'label': 'docs', 'onClick': function() { console.log('clicked ' + this.label); } }; _.bindAll(view); $('#docs').on('click', view.onClick); // → logs 'clicked docs' when the element is clicked
  • 18.
    _.get .Gets the propertyvalue of path on object. If the resolved value is undefined the defaultValue is used in its place. ._.get(object, path, [defaultValue]) .Returns:Returns the resolved Value. var object = { 'a': [{ 'b': { 'c': 3 } }] }; _.get(object, 'a[0].b.c'); // → 3 _.get(object, ['a', '0', 'b', 'c']); // → 3 _.get(object, 'a.b.c', 'default'); // → 'default'
  • 19.
    _template var compiled =_.template('hello <%= user %>!'); compiled({ 'user': 'fred' }); // → 'hello fred!' var compiled = _.template('<b><%- value %></b>'); compiled({ 'value': '<script>' }); // → '<b>&lt;script&gt;</b>' var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>'); compiled({ 'users': ['fred', 'barney'] }); // → '<li>fred</li><li>barney</li>' var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>'; var compiled = _.template(text, { 'imports': { 'jq': jQuery } }); compiled({ 'users': ['fred', 'barney'] }); // → '<li>fred</li><li>barney</li>'
  • 20.
    Reference API Documentaion :https://lodash.com/docs DevDocs: http://devdocs.io/lodash/