Underscore.js (1.7.0)-Collection (collections) (25)

Source: Internet
Author: User
Tags aliases

Audit function (array or object)

each _.each(list, iteratee, [context]) alias: ForEach
Iterates through all the elements in the list , sequentially outputting each element in sequence. If the context parameter is passed, the iteratee is bound to the context object. Each invocation of Iteratee will pass three parameters:(element, index, list). If list is a JavaScript object, theiteratee parameter is (value, key, list). Returns a list to make it easy to chain-call. (Note: If there is a native ForEach method, underscore uses it instead.) )

_.each ([1, 2, 3], alert);=> alerts each number in Turn..._.each ({one:1, two:2, three:3}, alert);=> alerts each Nu Mber value in turn ...

Note: Aggregate functions can be in arrays, objects, and class array objects, such as arguments, NodeList and similar data types work correctly. But it works by duck type, so avoid passing an object with an invariant length property (note: The Length property of an object or array is fixed). Each cycle cannot be broken-breaking, using _.find instead, which is also a good note.

Map _.map(list, iteratee, [context]) alias: collect
Each value in the list is mapped to a new array by a transform function (iteratee iterator) (Note: A new array is generated). If there is a native map method, the native map method is used instead. If list is a JavaScript object, theiteratee parameter is (value, key, list).

_.map ([1, 2, 3], function (num) {return num * 3;}); = = [3, 6, 9]_.map ({one:1, two:2, Three:3}, function (num, key) {return num * 3;}); = = [3, 6, 9]

Reduce _.reduce(list, iteratee, [memo], [context]) Aliases: inject, FOLDL
Aliases are inject and foldl, and the reduce method boils down the elements in the list to a single value. Memo is the initial value of the reduce function, and each step of reduce needs to be returned by Iteratee . This iteration passes 4 parameters:Memo, value and iteration of index(or key) and the last reference to the entire list.

If no memo is passed to the initial call to reduce ,iteratee is not called by the first element in the list. The first element replaces the memo parameter that is passed to the next element of the list calling iteratee .

var sum = _.reduce ([1, 2, 3], function (memo, NUM) {return memo + num;}, 0);=> 6

reduceright _.reduceRight(list, iteratee, memo, [context]) alias: foldr
reducright is the reduce function of the elements that are grouped from the right, and is replaced if there is a JavaScript version of reduceright1.8. Foldr is not as useful in JavaScript as other languages that have lazy calculations (Note: Lazy Evaluation: An evaluation strategy that evaluates an expression only if the value of the expression really needs it).

var list = [[0, 1], [2, 3], [4, 5]];var flat = _.reduceright (list, function (A, b) {return a.concat (b);}, []);=> [4, 5 , 2, 3, 0, 1]

Find _.find(list, predicate, [context]) alias: detect
Find each item in the list , returning the first element value to be detected by the predicate iteration function, if no value is passed to the test iterator undefined . If a matching element is found, the function returns immediately and does not traverse the entire list.

var even = _.find ([1, 2, 3, 4, 5, 6], function (num) {return num% 2 = = 0;}); = 2

Filter _.filter(list, predicate, [context]) aliases: Select
Iterates through each value in the list , returning the element value that contains all of the predicate truth detections. (Note: If there is a native filter method, the native filter method is used.) )

var evens = _.filter ([1, 2, 3, 4, 5, 6], function (num) {return num% 2 = = 0;}); = = [2, 4, 6]

where _.where(list, properties)
Iterates through each value in the list , returning an array that contains all the key- value pairs that contain the attributes listed in properties.

_.where (Listofplays, {author: "Shakespeare", year:1611});=> [{title: "Cymbeline", Author: "Shakespeare", year:1611} ,    {title: "The Tempest", Author: "Shakespeare", year:1611}]

Findwhere _.findWhere(list, properties)
Iterates through each value in the list , returning the first value of all key-value pairs that match properties listed in the property.

If no matching attribute is found, or the list is empty, then undefinedwill be returned.

_.findwhere (Publicservicepulitzers, {newsroom: "The New York Times"});=> {year:1918, Newsroom: "The New York Times", 
   reason: "For it public service ' in publishing in ' Many official reports,  documents and speeches by European Statesmen relating to the progress and  conduct of the War. "}

Reject _.reject(list, predicate, [context])
Returns a collection of elements in the list that are not detected by the predicate truth value, as opposed to filter .

var odds = _.reject ([1, 2, 3, 4, 5, 6], function (num) {return num% 2 = = 0;}); = = [1, 3, 5]

every _.every(list, [predicate], [context]) aliases: All
Returns trueif all elements in the list are detected by the predicate truth. (Note: If there is a native every method, the native everyis used.) )

_.every ([True, 1, NULL, ' yes '], _.identity);=> false

some _.some(list, [predicate], [context]) alias: any
Returns trueif any of the elements in the list are detected by the predicate truth. Once a qualifying element is found, the traversal of the list is interrupted directly. (Note: If there is a native some method, the native someis used.) )

_.some ([null, 0, ' yes ', false]);=> true

contains _.contains(list, value) aliases: include
Returns trueif the list contains the specified value (Note: use = = = detection). If the list is an array, the internal use of IndexOf is judged.

_.contains ([1, 2, 3], 3);=> true

Invoke _.invoke(list, methodName, *arguments)
Executes the methodName method on each element of the list . Any additional arguments passed to invoke ,invoke is passed to it when the methodName method is called.

_.invoke ([[5, 1, 7], [3, 2, 1]], ' sort ');=> [[1, 5, 7], [1, 2, 3]]

Pluck _.pluck(list, propertyName)
Pluck may be the simplified version of the use case model most commonly used by map , which extracts an attribute value from an array of objects and returns an array.

var stooges = [{name: ' Moe ', age:40}, {name: ' Larry ', age:50}, {name: ' Curly ', Age:60}];_.pluck (Stooges, ' name ');=> ["Moe", "Larry", "Curly"]

Max _.max(list, [iteratee], [context])
Returns the maximum value in the list . If you pass the iteratee parameter,iteratee will be used as the sort order for each value in the list . If list is empty, -infinitywill be returned, so you may need to check list with IsEmpty beforehand.

var stooges = [{name: ' Moe ', age:40}, {name: ' Larry ', age:50}, {name: ' Curly ', Age:60}];_.max (Stooges, function (Stooge) {return stooge.age;}); = = {Name: ' Curly ', age:60};

min _.min(list, [iteratee], [context])
Returns the minimum value in the list . If you pass the iteratee parameter,iteratee will be used as the sort order for each value in the list . If list is empty, -infinitywill be returned, so you may need to check list with IsEmpty beforehand.

var numbers = [Ten, 5, 2, 1000];_.min (numbers);=> 2

SortBy _.sortBy(list, iteratee, [context])
Returns a sorted copy of the list copy. If you pass the iteratee parameter,iteratee will be used as the sort order for each value in the list . Iterators can also be sorted by the name of the property of the string (for example, length).

_.sortby ([1, 2, 3, 4, 5, 6], function (num) {return math.sin (num);}); = = [5, 4, 6, 3, 1, 2]

groupBy _.groupBy(list, iteratee, [context])
Groups a collection into multiple collections, grouped by the results returned by iterator . If iterator is a string instead of a function, it will be grouped using iterator as the attribute name for each element.

_.groupby ([1.3, 2.1, 2.4], function (num) {return math.floor (num);}); + = {1: [1.3], 2: [2.1, 2.4]}_.groupby ([' One ', ' one ', ' ', ' three '], ' length ');=> {3: ["One", "one"], 5: ["Three"]}

Indexby _.indexBy(list, iteratee, [context])
Given a list, and a iterator function (or property name) that returns a key to each element in the list, returns an object for each item index. It's very much like GroupBy, but you can use Indexby when you know that your keys are unique.

var stooges = [{name: ' Moe ', age:40}, {name: ' Larry ', age:50}, {name: ' Curly ', Age:60}];_.indexby (Stooges, ' Age ');=> {  "Max": {name: ' Moe ', age:40},  "": {Name: ' Larry ', age:50},  "": {name: ' Curly ', age:60}}

CountBy _.countBy(list, iteratee, [context])
Sorts a list of groups, and returns a count of the number of objects in each group. Similar to groupBy, but does not return the value of the list, but returns the number of values in that group.

_.countby ([1, 2, 3, 4, 5], function (num) {  return num% 2 = = 0? ' Even ': ' odd ';}); = = {Odd:3, even:2}

Shuffle _.shuffle(list)
Returns a randomly ordered list copy, using Fisher-yates shuffle for random chaos.

_.shuffle ([1, 2, 3, 4, 5, 6]);=> [4, 1, 6, 3, 5, 2]

Sample _.sample(list, [n])
A random sample is generated from the list . Passing a number indicates that n random elements are returned from a list . Otherwise, a single random item is returned.

_.sample ([1, 2, 3, 4, 5, 6]);=> 4_.sample ([1, 2, 3, 4, 5, 6], 3);=> [1, 6, 2]

ToArray _.toArray(list)
Converting a list(any object that can be iterated) to an array is useful when converting arguments objects.

(function () {return _.toarray (arguments). Slice (1);}) (1, 2, 3, 4);=> [2, 3, 4]

size _.size(list)
Returns the length of the list .

_.size ({one:1, two:2, Three:3});=> 3

Partition _.partition(array, predicate)
Splits an array into twoarrays: The first array whose elements satisfy the predicate iteration function, and none of the second elements satisfies the predicate iteration function.

_.partition ([0, 1, 2, 3, 4, 5], isodd);=> [[1, 3, 5], [0, 2, 4]]

Underscore.js (1.7.0)-Collection (collections) (25)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.