Javascript: P12 Lodash
Javascript: P12 Lodash
p12
Lodash
AGENDA
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()
_.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)
Aliases
_.first
Arguments
1.array (Array): The array to query.
_.chunk()
_.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()
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
_.min(array) Computes the minimum value of array. If array is empty or falsey, undefined is returned.
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.
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()
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()
_.template([string=''], [options={}])
Arguments
[string=''] (string): The template string.
[options={}] (Object): The options object.
Returns
(Function): Returns the compiled template function.
example