[Understand Underscore and Lo-Dash] Collections _. each

Source: Internet
Author: User
Document directory
  • Aliases
  • Arguments
  • Returns
  • Aliases
  • Arguments
  • Returns
_. Each

Traverses the set and calls back each element in the set.

APILo-Dash

_. ForEach (collection [, callback = identity, thisArg])

Aliases

Each

Arguments
  1. collection (Array | Object | String): Set to be traversed
  2. [callback=identity] (Function): Functions called in each iteration
  3. [thisArg] (Arbitrary): BindcallbackOfthis
  4. callbackThree parameters are accepted:(value, index|key, collection)
Returns

(Array, Object, String): Returncollection.

Underscore

_. Each (list, iterator, [context])

Aliases

ForEach

Arguments
  1. list (Array | Object | String): Set to be traversed
  2. iterator (Function): Functions called in each iteration
  3. [context] (Arbitrary): BindcallbackOfthis
  4. iteratorThree parameters are accepted:(element|value, index|key, list)
Returns

(Array, Object, String): Returnundefined.

Note
  • Lo-Dash can omit the callback function, while Underscore must be passed in
  • Lo-Dash can be returned through callbackfalseEnd iteration ahead of schedule
  • Lo-Dash returns the Collection to allow chained operations. The returned value of Underscore is undefined.
ExampleLo-Dash
12345678
_. ForEach ([1, 2]) // => Returns [1, 2, 3] _ ([1, 2, 3]). forEach (alert ). join (','); // => alert returns '1, 2, 3 '_ for each number '_. forEach ({'one': 1, 'two': 2, 'three ': 3}, alert ); // => alert each number value (not necessarily executed in the defined order)
Underscore
1234
_. Each ([1, 2, 3], alert); // => alert each number _. each ({one: 1, two: 2, three: 3}, alert); // => alert each number value (not necessarily in the defined order)
SourceLo-Dash
12345678910111213141516
function forEach(collection, callback, thisArg) {    var index = -1,    length = collection ? collection.length : 0;    callback = callback && typeof thisArg == 'undefined' ? callback : lodash.createCallback(callback, thisArg);    if (typeof length == 'number') {        while (++index < length) {            if (callback(collection[index], index, collection) === false) {                break;            }        }    } else {        forOwn(collection, callback);    }    return collection;}
Underscore
12345678910111213141516
var each = _.each = _.forEach = function(obj, iterator, context) {    if (obj == null) return;    if (nativeForEach && obj.forEach === nativeForEach) {      obj.forEach(iterator, context);    } else if (obj.length === +obj.length) {      for (var i = 0, l = obj.length; i < l; i++) {        if (iterator.call(context, obj[i], i, obj) === breaker) return;      }    } else {      for (var key in obj) {        if (_.has(obj, key)) {          if (iterator.call(context, obj[key], key, obj) === breaker) return;        }      }    }  };
Additional
  • obj.length === +obj.length

+obj: Convert obj to a decimal number; otherwise, returnNaN. Therefore, the above judgment is equivalentobj.length && typeof obj.length == 'number'

  • if (iterator.call(context, obj[i], i, obj) === breaker) return;

breakerIs a pre-defined empty object ({}). Underscore is used internally to end the cycle in advance and is not made public to the public. In addition, because the object's===The object address is compared, so even if the user returns{}ifStill not valid

  • for inLoop does not traverse the non-enumerable attribute, so likeObjectOftoStringWill not be iterated

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.