_.assignIn(object, [sources])
This method is like _.assign except that it iterates over own and inherited source properties.
Note: This method mutates object.
4.0.0
_.extend
object (Object): The destination object.[sources] (...Object): The source objects.(Object): Returns object.
function Foo() { this.a = 1;}
function Bar() { this.c = 3;} Foo.prototype.b = 2;Bar.prototype.d = 4; _.assignIn({ 'a': 0 }, new Foo, new Bar);// => { 'a': 1, 'b': 2, 'c': 3, 'd': 4 }_.before(n, func)
Creates a function that invokes func, with the this binding and arguments of the created function, while it's called less than n times. Subsequent calls to the created function return the result of the last func invocation.
3.0.0
n (number): The number of calls at which func is no longer invoked.func (Function): The function to restrict.(Function): Returns the new restricted function.
jQuery(element).on('click', _.before(5, addContactToList));// => Allows adding up to 4 contacts to the list._.bind(func, thisArg, [partials])
Creates a function that invokes func with the this binding of thisArg and partials prepended to the arguments it receives.
The _.bind.placeholder value, which defaults to _ in monolithic builds, may be used as a placeholder for partially applied arguments.
Note: Unlike native Function#bind, this method doesn't set the "length" property of bound functions.
0.1.0
func (Function): The function to bind.thisArg (*): The this binding of func.[partials] (...*): The arguments to be partially applied.(Function): Returns the new bound function.
function greet(greeting, punctuation) { return greeting + ' ' + this.user + punctuation;}
var object = { 'user': 'fred' };
var bound = _.bind(greet, object, 'hi');
bound('!');// => 'hi fred!' // Bound with placeholders.
var bound = _.bind(greet, object, _, '!');
bound('hi');// => 'hi fred!'_.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.
1.3.0
value (*): The value to wrap.(Object): Returns the new lodash wrapper instance.
var users = [ { 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'pebbles', 'age': 1 }];
var youngest = _ .chain(users) .sortBy('age') .map(function(o) { return o.user + ' is ' + o.age; }) .head() .value();// => 'pebbles is 1'_.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.
0.1.0
value (*): The value to clone.(*): Returns the cloned value.
var objects = [{ 'a': 1 }, { 'b': 2 }];
var shallow = _.clone(objects);
console.log(shallow[0] === objects[0]);// => true_.compact(array)
Creates an array with all falsey values removed. The values false, null, 0, "", undefined, and NaN are falsey.
0.1.0
array (Array): The array to compact.(Array): Returns the new array of filtered values.
_.compact([0, 1, false, 2, '', 3]);// => [1, 2, 3]_.concat(array, [values])
Creates a new array concatenating array with any additional arrays and/or values.
4.0.0
array (Array): The array to concatenate.[values] (...*): The values to concatenate.(Array): Returns the new concatenated array.
var array = [1];
var other = _.concat(array, 2, [3], [[4]]);
console.log(other);// => [1, 2, 3, [4]]
console.log(array);// => [1]_.create(prototype, [properties])
Creates an object that inherits from the prototype object. If a properties object is given, its own enumerable string keyed properties are assigned to the created object.
2.3.0
prototype (Object): The object to inherit from.[properties] (Object): The properties to assign to the object.(Object): Returns the new object.
function Shape() { this.x = 0; this.y = 0;}
function Circle() { Shape.call(this);} Circle.prototype = _.create(Shape.prototype, { 'constructor': Circle});
var circle = new Circle;circle instanceof Circle;// => true circle instanceof Shape;// => true_.defaults(object, [sources])
Assigns own and inherited enumerable string keyed properties of source objects to the destination object for all destination properties that resolve to undefined. Source objects are applied from left to right. Once a property is set, additional values of the same property are ignored.
Note: This method mutates object.
0.1.0
object (Object): The destination object.[sources] (...Object): The source objects.(Object): Returns object.
_.defaults({ 'a': 1 }, { 'b': 2 }, { 'a': 3 });// => { 'a': 1, 'b': 2 }_.defer(func, [args])
Defers invoking the func until the current call stack has cleared. Any additional arguments are provided to func when it's invoked.
0.1.0
func (Function): The function to defer.[args] (...*): The arguments to invoke func with.(number): Returns the timer id.
_.defer(function(text) { console.log(text);}, 'deferred');// => Logs 'deferred' after one millisecond._.delay(func, wait, [args])
Invokes func after wait milliseconds. Any additional arguments are provided to func when it's invoked.
0.1.0
func (Function): The function to delay.wait (number): The number of milliseconds to delay invocation.[args] (...*): The arguments to invoke func with.(number): Returns the timer id.
_.delay(function(text) { console.log(text);}, 1000, 'later');// => Logs 'later' after one second._.escape([string=''])
Converts the characters "&", "<", ">", '"', and "'" in string to their corresponding HTML entities.
Note: No other characters are escaped. To escape additional characters use a third-party library like he.
Though the ">" character is escaped for symmetry, characters like ">" and "/" don't need escaping in HTML and have no special meaning unless they're part of a tag or unquoted attribute value. See Mathias Bynens's article (under "semi-related fun fact") for more details.
When working with HTML you should always quote attribute values to reduce XSS vectors.
0.1.0
[string=''] (string): The string to escape.(string): Returns the escaped string.
_.escape('fred, barney, & pebbles');// => 'fred, barney, &amp; pebbles'_.every(collection, [predicate=_.identity])
Checks if predicate returns truthy for all elements of collection. Iteration is stopped once predicate returns falsey. The predicate is invoked with three arguments: (value, index|key, collection).
Note: This method returns true for empty collections because everything is true of elements of empty collections.
0.1.0
collection (Array|Object): The collection to iterate over.[predicate=_.identity] (Function): The function invoked per iteration.(boolean): Returns true if all elements pass the predicate check, else false.
_.every([true, 1, null, 'yes'], Boolean);// => false
var users = [ { 'user': 'barney', 'age': 36, 'active': false },
{ 'user': 'fred', 'age': 40, 'active': false }]; // The `_.matches` iteratee shorthand._.every(users, { 'user': 'barney', 'active': false });// => false // The `_.matchesProperty` iteratee shorthand._.every(users, ['active', false]);// => true // The `_.property` iteratee shorthand._.every(users, 'active');// => false_.filter(collection, [predicate=_.identity])
Iterates over elements of collection, returning an array of all elements predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).
Note: Unlike _.remove, this method returns a new array.
0.1.0
collection (Array|Object): The collection to iterate over.[predicate=_.identity] (Function): The function invoked per iteration.(Array): Returns the new filtered array.
var users = [ { 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false }]; _.filter(users, function(o) { return !o.active; });// => objects for ['fred'] // The `_.matches` iteratee shorthand._.filter(users, { 'age': 36, 'active': true });// => objects for ['barney'] // The `_.matchesProperty` iteratee shorthand._.filter(users, ['active', false]);// => objects for ['fred'] // The `_.property` iteratee shorthand._.filter(users, 'active');// => objects for ['barney']_.find(collection, [predicate=_.identity], [fromIndex=0])
Iterates over elements of collection, returning the first element predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).
0.1.0
collection (Array|Object): The collection to inspect.[predicate=_.identity] (Function): The function invoked per iteration.[fromIndex=0] (number): The index to search from.(*): Returns the matched element, else undefined.
var users = [ { 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false },
{ 'user': 'pebbles', 'age': 1, 'active': true }]; _.find(users, function(o) { return o.age < 40; });// => object for 'barney' // The `_.matches` iteratee shorthand._.find(users, { 'age': 1, 'active': true });// => object for 'pebbles' // The `_.matchesProperty` iteratee shorthand._.find(users, ['active', false]);// => object for 'fred' // The `_.property` iteratee shorthand._.find(users, 'active');// => object for 'barney'_.flatten(array)
Flattens array a single level deep.
0.1.0
array (Array): The array to flatten.(Array): Returns the new flattened array.
_.flatten([1, [2, [3, [4]], 5]]);// => [1, 2, [3, [4]], 5]_.flattenDeep(array)
Recursively flattens array.
3.0.0
array (Array): The array to flatten.(Array): Returns the new flattened array.
_.flattenDeep([1, [2, [3, [4]], 5]]);// => [1, 2, 3, 4, 5]_.forEach(collection, [iteratee=_.identity])
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.
Note: As with other "Collections" methods, objects with a "length" property are iterated like arrays. To avoid this behavior use _.forIn or _.forOwn for object iteration.
0.1.0
_.each
collection (Array|Object): The collection to iterate over.[iteratee=_.identity] (Function): The function invoked per iteration.(*): Returns collection.
_.forEach([1, 2], function(value) { console.log(value);});// => Logs `1` then `2`. _.forEach({ 'a': 1, 'b': 2 }, function(value, key) { console.log(key);});// => Logs 'a' then 'b' (iteration order is not guaranteed)._.has(object, path)
Checks if path is a direct property of object.
0.1.0
object (Object): The object to query.path (Array|string): The path to check.(boolean): Returns true if path exists, else false.
var object = { 'a': { 'b': 2 } };
var other = _.create({ 'a': _.create({ 'b': 2 }) }); _.has(object, 'a');// => true _.has(object, 'a.b');// => true _.has(object, ['a', 'b']);// => true _.has(other, 'a');// => false_.head(array)
Gets the first element of array.
0.1.0
_.first
array (Array): The array to query.(*): Returns the first element of array.
_.head([1, 2, 3]);// => 1 _.head([]);// => undefined_.identity(value)
This method returns the first argument it receives.
0.1.0
value (*): Any value.(*): Returns value.
var object = { 'a': 1 };
console.log(_.identity(object) === object);// => true_.indexOf(array, value, [fromIndex=0])
Gets the index at which the first occurrence of value is found in array using SameValueZero for equality comparisons. If fromIndex is negative, it's used as the offset from the end of array.
0.1.0
array (Array): The array to inspect.value (*): The value to search for.[fromIndex=0] (number): The index to search from.(number): Returns the index of the matched value, else -1.
_.indexOf([1, 2, 1, 2], 2);// => 1 // Search from the `fromIndex`._.indexOf([1, 2, 1, 2], 2, 2);// => 3_.isArguments(value)
Checks if value is likely an arguments object.
0.1.0
value (*): The value to check.(boolean): Returns true if value is an arguments object, else false.
_.isArguments(function() { return arguments; }());// => true _.isArguments([1, 2, 3]);// => false_.isArray(value)
Checks if value is classified as an Array object.
0.1.0
value (*): The value to check.(boolean): Returns true if value is an array, else false.
_.isArray([1, 2, 3]);// => true _.isArray(document.body.children);// => false _.isArray('abc');// => false _.isArray(_.noop);// => false_.isBoolean(value)
Checks if value is classified as a boolean primitive or object.
0.1.0
value (*): The value to check.(boolean): Returns true if value is a boolean, else false.
_.isBoolean(false);// => true _.isBoolean(null);// => false_.isDate(value)
Checks if value is classified as a Date object.
0.1.0
value (*): The value to check.(boolean): Returns true if value is a date object, else false.
_.isDate(new Date);// => true _.isDate('Mon April 23 2012');// => false_.isEmpty(value)
Checks if value is an empty object, collection, map, or set.
Objects are considered empty if they have no own enumerable string keyed properties.
Array-like values such as arguments objects, arrays, buffers, strings, or jQuery-like collections are considered empty if they have a length of 0. Similarly, maps and sets are considered empty if they have a size of 0.
0.1.0
value (*): The value to check.(boolean): Returns true if value is empty, else false.
_.isEmpty(null);// => true _.isEmpty(true);// => true _.isEmpty(1);// => true _.isEmpty([1, 2, 3]);// => false _.isEmpty({ 'a': 1 });// => false_.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. ===.
0.1.0
value (*): The value to compare.other (*): The other value to compare.(boolean): Returns true if the values are equivalent, else false.
var object = { 'a': 1 };
var other = { 'a': 1 }; _.isEqual(object, other);// => true object === other;// => false_.isFinite(value)
Checks if value is a finite primitive number.
Note: This method is based on Number.isFinite.
0.1.0
value (*): The value to check.(boolean): Returns true if value is a finite number, else false.
_.isFinite(3);// => true _.isFinite(Number.MIN_VALUE);// => true _.isFinite(Infinity);// => false _.isFinite('3');// => false_.isFunction(value)
Checks if value is classified as a Function object.
0.1.0
value (*): The value to check.(boolean): Returns true if value is a function, else false.
_.isFunction(_);// => true _.isFunction(/abc/);// => false_.isNaN(value)
Checks if value is NaN.
Note: This method is based on Number.isNaN and is not the same as global isNaN which returns true for undefined and other non-number values.
0.1.0
value (*): The value to check.(boolean): Returns true if value is NaN, else false.
_.isNaN(NaN);// => true _.isNaN(new Number(NaN));// => true
isNaN(undefined);// => true _.isNaN(undefined);// => false_.isNull(value)
Checks if value is null.
0.1.0
value (*): The value to check.(boolean): Returns true if value is null, else false.
_.isNull(null);// => true _.isNull(void 0);// => false_.isNumber(value)
Checks if value is classified as a Number primitive or object.
Note: To exclude Infinity, -Infinity, and NaN, which are classified as numbers, use the _.isFinite method.
0.1.0
value (*): The value to check.(boolean): Returns true if value is a number, else false.
_.isNumber(3);// => true _.isNumber(Number.MIN_VALUE);// => true _.isNumber(Infinity);// => true _.isNumber('3');// => false_.isObject(value)
Checks if value is the language type of Object. (e.g. arrays, functions, objects, regexes, new Number(0), and new String(''))
0.1.0
value (*): The value to check.(boolean): Returns true if value is an object, else false.
_.isObject({});// => true _.isObject([1, 2, 3]);// => true _.isObject(_.noop);// => true _.isObject(null);// => false_.isRegExp(value)
Checks if value is classified as a RegExp object.
0.1.0
value (*): The value to check.(boolean): Returns true if value is a regexp, else false.
_.isRegExp(/abc/);// => true _.isRegExp('/abc/');// => false_.isString(value)
Checks if value is classified as a String primitive or object.
0.1.0
value (*): The value to check.(boolean): Returns true if value is a string, else false.
_.isString('abc');// => true _.isString(1);// => false_.isUndefined(value)
Checks if value is undefined.
0.1.0
value (*): The value to check.(boolean): Returns true if value is undefined, else false.
_.isUndefined(void 0);// => true _.isUndefined(null);// => false_.iteratee([func=_.identity])
Creates a function that invokes func with the arguments of the created function. If func is a property name, the created function returns the property value for a given element. If func is an array or object, the created function returns true for elements that contain the equivalent source properties, otherwise it returns false.
4.0.0
[func=_.identity] (*): The value to convert to a callback.(Function): Returns the callback.
var users = [ { 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false }]; // The `_.matches` iteratee shorthand._.filter(users, _.iteratee({ 'user': 'barney', 'active': true }));// => [{ 'user': 'barney', 'age': 36, 'active': true }] // The `_.matchesProperty` iteratee shorthand._.filter(users, _.iteratee(['user', 'fred']));// => [{ 'user': 'fred', 'age': 40 }] // The `_.property` iteratee shorthand._.map(users, _.iteratee('user'));// => ['barney', 'fred'] // Create custom iteratee shorthands._.iteratee = _.wrap(_.iteratee, function(iteratee, func) { return !_.isRegExp(func) ? iteratee(func) : function(string) { return func.test(string); };}); _.filter(['abc', 'def'], /ef/);// => ['def']_.keys(object)
Creates an array of the own enumerable property names of object.
Note: Non-object values are coerced to objects. See the ES spec for more details.
0.1.0
object (Object): The object to query.(Array): Returns the array of property names.
function Foo() { this.a = 1; this.b = 2;} Foo.prototype.c = 3; _.keys(new Foo);// => ['a', 'b'] (iteration order is not guaranteed) _.keys('hi');// => ['0', '1']_.last(array)
Gets the last element of array.
0.1.0
array (Array): The array to query.(*): Returns the last element of array.
_.last([1, 2, 3]);// => 3_.map(collection, [iteratee=_.identity])
Creates an array of values by running each element in collection thru iteratee. The iteratee is invoked with three arguments:
(value, index|key, collection).
Many lodash methods are guarded to work as iteratees for methods like _.every, _.filter, _.map, _.mapValues, _.reject, and _.some.
The guarded methods are:ary, chunk, curry, curryRight, drop, dropRight, every, fill, invert, parseInt, random, range, rangeRight, repeat, sampleSize, slice, some, sortBy, split, take, takeRight, template, trim, trimEnd, trimStart, and words
0.1.0
collection (Array|Object): The collection to iterate over.[iteratee=_.identity] (Function): The function invoked per iteration.(Array): Returns the new mapped array.
function square(n) { return n * n;} _.map([4, 8], square);// => [16, 64] _.map({ 'a': 4, 'b': 8 }, square);// => [16, 64] (iteration order is not guaranteed)
var users = [ { 'user': 'barney' },
{ 'user': 'fred' }]; // The `_.property` iteratee shorthand._.map(users, 'user');// => ['barney', 'fred']_.matches(source)
Creates a function that performs a partial deep comparison between a given object and source, returning true if the given object has equivalent property values, else false.
Note: The created function is equivalent to _.isMatch with source partially applied.
Partial comparisons will match empty array and empty object source values against any array or object value, respectively. See _.isEqual for a list of supported value comparisons.
3.0.0
source (Object): The object of property values to match.(Function): Returns the new spec function.
var objects = [ { 'a': 1, 'b': 2, 'c': 3 },
{ 'a': 4, 'b': 5, 'c': 6 }]; _.filter(objects, _.matches({ 'a': 4, 'c': 6 }));// => [{ 'a': 4, 'b': 5, 'c': 6 }]_.max(array)
Computes the maximum value of array. If array is empty or falsey, undefined is returned.
0.1.0
array (Array): The array to iterate over.(*): Returns the maximum value.
_.max([4, 2, 8, 6]);// => 8 _.max([]);// => undefined_.min(array)
Computes the minimum value of array. If array is empty or falsey, undefined is returned.
0.1.0
array (Array): The array to iterate over.(*): Returns the minimum value.
_.min([4, 2, 8, 6]);// => 2 _.min([]);// => undefined_.mixin([object=lodash], source, [options={}])
Adds all own enumerable string keyed function properties of a source object to the destination object. If object is a function, then methods are added to its prototype as well.
Note: Use _.runInContext to create a pristine lodash function to avoid conflicts caused by modifying the original.
0.1.0
[object=lodash] (Function|Object): The destination object.source (Object): The object of functions to add.[options={}] (Object): The options object.[options.chain=true] (boolean): Specify whether mixins are chainable.(*): Returns object.
function vowels(string) { return _.filter(string, function(v) { return /[aeiou]/i.test(v); });} _.mixin({ 'vowels': vowels });_.vowels('fred');// => ['e']
_('fred').vowels().value();// => ['e'] _.mixin({ 'vowels': vowels }, { 'chain': false });
_('fred').vowels();// => ['e']_.negate(predicate)
Creates a function that negates the result of the predicate func. The func predicate is invoked with the this binding and arguments of the created function.
3.0.0
predicate (Function): The predicate to negate.(Function): Returns the new negated function.
function isEven(n) { return n % 2 == 0;} _.filter([1, 2, 3, 4, 5, 6], _.negate(isEven));// => [1, 3, 5]_.noConflict()
Reverts the _ variable to its previous value and returns a reference to the lodash function.
0.1.0
(Function): Returns the lodash function.
var lodash = _.noConflict();_.noop()
This method returns undefined.
2.3.0
_.times(2, _.noop);// => [undefined, undefined]_.once(func)
Creates a function that is restricted to invoking func once. Repeat calls to the function return the value of the first invocation. The func is invoked with the this binding and arguments of the created function.
0.1.0
func (Function): The function to restrict.(Function): Returns the new restricted function.
var initialize = _.once(createApplication);
initialize();
initialize();// => `createApplication` is invoked once_.pick(object, [paths])
Creates an object composed of the picked object properties.
0.1.0
object (Object): The source object.[paths] (...(string|string[])): The property paths to pick.(Object): Returns the new object.
var object = { 'a': 1, 'b': '2', 'c': 3 }; _.pick(object, ['a', 'c']);// => { 'a': 1, 'c': 3 }_.reduce(collection, [iteratee=_.identity], [accumulator])
Reduces collection to a value which is the accumulated result of running each element in collection thru iteratee, where each successive invocation is supplied the return value of the previous. If accumulator is not given, the first element of collection is used as the initial value. The iteratee is invoked with four arguments:
(accumulator, value, index|key, collection).
Many lodash methods are guarded to work as iteratees for methods like _.reduce, _.reduceRight, and _.transform.
The guarded methods are:assign, defaults, defaultsDeep, includes, merge, orderBy, and sortBy
0.1.0
collection (Array|Object): The collection to iterate over.[iteratee=_.identity] (Function): The function invoked per iteration.[accumulator] (*): The initial value.(*): Returns the accumulated value.
_.reduce([1, 2], function(sum, n) { return sum + n;}, 0);// => 3 _.reduce({ 'a': 1, 'b': 2, 'c': 1 }, function(result, value, key) { (result[value] || (result[value] = [])).push(key); return result;}, {});// => { '1': ['a', 'c'], '2': ['b'] } (iteration order is not guaranteed)_.result(object, path, [defaultValue])
This method is like _.get except that if the resolved value is a function it's invoked with the this binding of its parent object and its result is returned.
0.1.0
object (Object): The object to query.path (Array|string): The path of the property to resolve.[defaultValue] (*): The value returned for undefined resolved values.(*): Returns the resolved value.
var object = { 'a': [{ 'b': { 'c1': 3, 'c2': _.constant(4) } }] }; _.result(object, 'a[0].b.c1');// => 3 _.result(object, 'a[0].b.c2');// => 4 _.result(object, 'a[0].b.c3', 'default');// => 'default' _.result(object, 'a[0].b.c3', _.constant('default'));// => 'default'_.size(collection)
Gets the size of collection by returning its length for array-like values or the number of own enumerable string keyed properties for objects.
0.1.0
collection (Array|Object|string): The collection to inspect.(number): Returns the collection size.
_.size([1, 2, 3]);// => 3 _.size({ 'a': 1, 'b': 2 });// => 2 _.size('pebbles');// => 7_.slice(array, [start=0], [end=array.length])
Creates a slice of array from start up to, but not including, end.
Note: This method is used instead of Array#slice to ensure dense arrays are returned.
3.0.0
array (Array): The array to slice.[start=0] (number): The start position.[end=array.length] (number): The end position.(Array): Returns the slice of array.
_.some(collection, [predicate=_.identity])
Checks if predicate returns truthy for any element of collection. Iteration is stopped once predicate returns truthy. The predicate is invoked with three arguments: (value, index|key, collection).
0.1.0
collection (Array|Object): The collection to iterate over.[predicate=_.identity] (Function): The function invoked per iteration.(boolean): Returns true if any element passes the predicate check, else false.
_.some([null, 0, 'yes', false], Boolean);// => true
var users = [ { 'user': 'barney', 'active': true },
{ 'user': 'fred', 'active': false }]; // The `_.matches` iteratee shorthand._.some(users, { 'user': 'barney', 'active': false });// => false // The `_.matchesProperty` iteratee shorthand._.some(users, ['active', false]);// => true // The `_.property` iteratee shorthand._.some(users, 'active');// => true_.sortBy(collection, [iteratees=[_.identity]])
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).
0.1.0
collection (Array|Object): The collection to iterate over.[iteratees=[_.identity]] (...(Function|Function[])): The iteratees to sort by.(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, [function(o) { return o.user; }]);// => objects for [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]] _.sortBy(users, ['user', 'age']);// => objects for [['barney', 34], ['barney', 36], ['fred', 40], ['fred', 48]]_.tap(value, interceptor)
This method invokes interceptor and returns value. The interceptor is invoked with one argument; (value). The purpose of this method is to "tap into" a method chain sequence in order to modify intermediate results.
0.1.0
value (*): The value to provide to interceptor.interceptor (Function): The function to invoke.(*): Returns value.
_([1, 2, 3]) .tap(function(array) {
// Mutate input array.
array.pop(); }) .reverse() .value();// => [2, 1]_.thru(value, interceptor)
This method is like _.tap except that it returns the result of interceptor. The purpose of this method is to "pass thru" values replacing intermediate results in a method chain sequence.
3.0.0
value (*): The value to provide to interceptor.interceptor (Function): The function to invoke.(*): Returns the result of interceptor.
_(' abc ') .chain() .trim() .thru(function(value) { return [value]; }) .value();// => ['abc']_.toArray(value)
Converts value to an array.
0.1.0
value (*): The value to convert.(Array): Returns the converted array.
_.toArray({ 'a': 1, 'b': 2 });// => [1, 2] _.toArray('abc');// => ['a', 'b', 'c'] _.toArray(1);// => [] _.toArray(null);// => []_.uniqueId([prefix=''])
Generates a unique ID. If prefix is given, the ID is appended to it.
0.1.0
[prefix=''] (string): The value to prefix the ID with.(string): Returns the unique ID.
_.uniqueId('contact_');// => 'contact_104' _.uniqueId();// => '105'_.values(object)
Creates an array of the own enumerable string keyed property values of object.
Note: Non-object values are coerced to objects.
0.1.0
object (Object): The object to query.(Array): Returns the array of property values.
function Foo() { this.a = 1; this.b = 2;} Foo.prototype.c = 3; _.values(new Foo);// => [1, 2] (iteration order is not guaranteed) _.values('hi');// => ['h', 'i']
Report a Bug