0% found this document useful (0 votes)
13K views24 pages

Javascript: P12 Lodash

The document provides an overview of various Lodash collection, function, object, string, and array utility methods. It summarizes the purpose and arguments of methods like _.each(), _.map(), _.head(), _.chunk(), _.compact(), _.drop(), math methods, _.partial(), _.flow(), _.assign(), _.extend(), _.merge(), _.camelCase(), _.trim() and _.words(), chaining with _chain(), sorting with _sortBy(), and templating with _.template().
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13K views24 pages

Javascript: P12 Lodash

The document provides an overview of various Lodash collection, function, object, string, and array utility methods. It summarizes the purpose and arguments of methods like _.each(), _.map(), _.head(), _.chunk(), _.compact(), _.drop(), math methods, _.partial(), _.flow(), _.assign(), _.extend(), _.merge(), _.camelCase(), _.trim() and _.words(), chaining with _chain(), sorting with _sortBy(), and templating with _.template().
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

JavaScript

p12
Lodash
AGENDA

Collection Function Object String Array


_.each() (_.forEach)

Iterates over elements of collection and invokes iteratee for each element. The iteratee is invoked with three arguments: (value,
index|key, collection). Iteratee functions may exit iteration early by explicitly returning false

_.forEach(collection, [iteratee=_.identity])

Aliases
_.each

Arguments
1.collection (Array|Object): The collection to iterate over.
2.[iteratee=_.identity] (Function): The function invoked per iteration.
_.map()

Creates an array of values by running each element in collection thru iteratee.


The iteratee is invoked with three arguments:
(value, index|key, collection).

_.map(collection, [iteratee=_.identity])

Arguments
1.collection (Array|Object): The collection to iterate over.
2.[iteratee=_.identity] (Function):
The function invoked per iteration.
_.head()

_.head(array)

Gets the first element of array.

Aliases
_.first
Arguments
1.array (Array): The array to query.
_.chunk()

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.

_.chunk(array, [size=1])

Arguments
1.array (Array): The array to process.
2.[size=1] (number): The length of each chunk

Returns
(Array): Returns the new array of chunks.
_.compact()

Creates an array with all falsey values removed. The values false, null, 0, "", undefined, and NaN are falsey.

Arguments
1.array (Array): The array to compact.

Returns
(Array): Returns the new array of filtered values.
_.drop()

Creates a slice of array with n elements dropped from the beginning.

_.drop(array, [n=1]) _.dropRight(array, [n=1])

Arguments
1.array (Array): The array to query.
2.[n=1] (number): The number of elements to drop.

Returns
(Array): Returns the slice of array.
“Math” methods

_.add(augend, addend) Adds two numbers.

_.ceil(number, [precision=0]) Computes number rounded up to precision.

_.divide(dividend, divisor) Divide two numbers.

_.min(array) Computes the minimum value of array. If array is empty or falsey, undefined is returned.

_.multiply(multiplier, multiplicand) Multiply two numbers.

_.sum(array) Computes the sum of the values in array.


_.partial()

Creates a function that invokes func with partials prepended to the arguments it receives.


This method is like _.bind except it does not alter the this binding.

The _.partial.placeholder value, which defaults to _ in monolithic builds,


may be used as a placeholder for partially applied arguments.

Arguments
1.func (Function): The function to partially apply arguments to.
2.[partials] (...*): The arguments to be partially applied.

Returns
(Function): Returns the new partially applied function.
_.flow()

_.flow([funcs])

Creates a function that returns the result of invoking the given functions
with the this binding of the created function, where each successive invocation
is supplied the return value of the previous.

Arguments
1.[funcs] (...(Function|Function[])): The functions to invoke.

Returns
(Function): Returns the new composite function.
_.assign()

_.assign(object, [sources])

Assigns own enumerable string keyed properties of source objects to the destination object.
Source objects are applied from left to right. Subsequent sources overwrite property
assignments of previous sources.

Arguments
object (Object): The destination object.
[sources] (...Object): The source objects.

Returns
(Object): Returns object.
example
_.extend()

_.assignIn(object, [sources])

This method is like _.assign except that it iterates over own and inherited source properties.

Note: This method mutates object.

Aliases
_.extend
Arguments
1.object (Object): The destination object.
2.[sources] (...Object): The source objects.

Returns
(Object): Returns object.
Example
example
_.merge()

This method is like _.assign except that it recursively merges own and inherited enumerable
string keyed properties of source objects into the destination object.
Source properties that resolve to undefined are skipped if a destination value exists.
Array and plain object properties are merged recursively. Other objects and value
types are overridden by assignment. Source objects are applied from left to right.
Subsequent sources overwrite property assignments of previous sources.

Arguments
1.object (Object): The destination object.
2.[sources] (...Object): The source objects.

Returns
(Object): Returns object.
_.camelCase()

_.camelCase([string=''])

Converts string to camel case.

Arguments
[string=''] (string): The string to convert.

Returns
(string): Returns the camel cased string.
_.trim() & _.words()

_.trim([string=''], [chars=whitespace]) _.words([string=''], [pattern])

Removes leading and trailing whitespace or specified Splits string into an array of its words.
characters from string.
Arguments Arguments
[string=''] (string): The string to trim. [string=''] (string): The string to inspect.
[chars=whitespace] (string): The characters [pattern] (RegExp|string): The pattern to match
to trim. words.

Returns Returns
(string): Returns the trimmed string. (Array): Returns the words of string.

_.trim('  abc  '); _.words('fred, barney, & pebbles');
// => 'abc' // => ['fred', 'barney', 'pebbles']
 _.trim('-_-abc-_-', '_-');  
// => 'abc' _.words('fred, barney, & pebbles', /[^, ]+/g);
 _.map(['  foo  ', '  bar  '], _.trim); // => ['fred', 'barney', '&', 'pebbles']
// => ['foo', 'bar']
_chain()

_.chain(value)

Creates a lodash wrapper instance that wraps value with explicit method chain sequences enabled.
The result of such sequences must be unwrapped with _#value.

Arguments
var users = [
value (*): The value to wrap.
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
Returns
{ 'user': 'pebbles', 'age': 1 }
(Object): Returns the new lodash wrapper instance.
];

var youngest = _
.chain(users)
.sortBy('age')
.map(function(o) { return o.user + ' is '
+ o.age;
})
.head()
.value();
_sortBy

Creates an array of elements, sorted in ascending order by the results of running each element in a collection thru
each iteratee. This method performs a stable sort, that is, it preserves the original sort order of equal elements. The
iteratees are invoked with one argument: (value).

Arguments
1.collection (Array|Object): The collection to iterate over.
2.[iteratees=[_.identity]] (...(Function|Function[])):
The iteratees to sort by.

Returns
(Array): Returns the new sorted array. var users = [
  { 'user': 'fred',   'age': 48 },
  { 'user': 'barney', 'age': 36 },
  { 'user': 'fred',   'age': 40 },
  { 'user': 'barney', 'age': 34 }
];
  
_.sortBy(users, ['user', 'age']);
// => objects for [['barney', 34], ['barney', 36], ['fred', 40], ['fre
d', 48]]
_.template()

<% %> - to execute some code


<%= %> - to print some value in template
<%- %> - to print some values HTML escaped

var template = _.template('<div><%= name %> </div>');


template({name: " Max"})

"<div> Max </div>"


_.template()

_.template([string=''], [options={}])

Creates a compiled template function that can interpolate data properties in


"interpolate" delimiters, HTML-escape interpolated data properties in "escape" delimiters,
and execute JavaScript in "evaluate" delimiters. Data properties may be accessed as free variables
in the template. If a setting object is given, it takes precedence over _.templateSettings values.

Arguments
[string=''] (string): The template string.
[options={}] (Object): The options object.
Returns
(Function): Returns the compiled template function.
example

<script type="text/template" id="menu-template">


<div class="menu">
<span class="title"><%-title%>
</span>
</div>
</script>

var template = document.getElementById('menu-template').innerHTML;


example

You might also like