mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-07 10:07:48 +00:00
started passing in the collection as the third argument to _.each iterators (Issue #1)
This commit is contained in:
@@ -171,7 +171,9 @@
|
|||||||
<br />
|
<br />
|
||||||
Iterates over a <b>list</b> of elements, yielding each in turn to an <b>iterator</b>
|
Iterates over a <b>list</b> of elements, yielding each in turn to an <b>iterator</b>
|
||||||
function. The <b>iterator</b> is bound to the <b>context</b> object, if one is
|
function. The <b>iterator</b> is bound to the <b>context</b> object, if one is
|
||||||
passed. If <b>list</b> is a JavaScript object, a pair with <b>key</b>
|
passed. Each invocation of <b>iterator</b> is called with three arguments,
|
||||||
|
<i>element</i>, <i>index</i>, and the <b>list</b>.
|
||||||
|
If <b>list</b> is a JavaScript object, a pair with <b>key</b>
|
||||||
and <b>value</b> properties will be yielded. If the list has an <b>each</b>
|
and <b>value</b> properties will be yielded. If the list has an <b>each</b>
|
||||||
method of its own, it will be used instead. Delegates to the native
|
method of its own, it will be used instead. Delegates to the native
|
||||||
<b>forEach</b> function if it exists.
|
<b>forEach</b> function if it exists.
|
||||||
|
|||||||
@@ -29,6 +29,10 @@ $(document).ready(function() {
|
|||||||
_.each(obj, function(pair){ answers.push(pair.key); });
|
_.each(obj, function(pair){ answers.push(pair.key); });
|
||||||
equals(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.');
|
equals(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.');
|
||||||
delete obj.constructor.prototype.four;
|
delete obj.constructor.prototype.four;
|
||||||
|
|
||||||
|
answer = null;
|
||||||
|
_.each([1, 2, 3], function(num, index, arr){ if (_.include(arr, num)) answer = true; });
|
||||||
|
ok(answer, 'can reference the original collection from inside the iterator');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('collections: map', function() {
|
test('collections: map', function() {
|
||||||
|
|||||||
@@ -38,16 +38,16 @@
|
|||||||
if (obj.forEach) {
|
if (obj.forEach) {
|
||||||
obj.forEach(iterator, context);
|
obj.forEach(iterator, context);
|
||||||
} else if (obj.length) {
|
} else if (obj.length) {
|
||||||
for (var i=0, l = obj.length; i<l; i++) iterator.call(context, obj[i], i);
|
for (var i=0, l = obj.length; i<l; i++) iterator.call(context, obj[i], i, obj);
|
||||||
} else if (obj.each) {
|
} else if (obj.each) {
|
||||||
obj.each(function(value) { iterator.call(context, value, index++); });
|
obj.each(function(value) { iterator.call(context, value, index++, obj); });
|
||||||
} else {
|
} else {
|
||||||
var i = 0;
|
var i = 0;
|
||||||
for (var key in obj) if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
for (var key in obj) if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||||
var value = obj[key], pair = [key, value];
|
var value = obj[key], pair = [key, value];
|
||||||
pair.key = key;
|
pair.key = key;
|
||||||
pair.value = value;
|
pair.value = value;
|
||||||
iterator.call(context, pair, i++);
|
iterator.call(context, pair, i++, obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user