Issue #2 -- each now calls iterator with (value, key, list) when iterating over javascript objects

This commit is contained in:
Jeremy Ashkenas
2009-10-29 10:46:53 -04:00
parent 8e7b8d2dea
commit 42637b5880
3 changed files with 18 additions and 17 deletions

View File

@@ -171,10 +171,9 @@
<br />
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
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>
passed. Each invocation of <b>iterator</b> is called with three arguments:
<tt>(element, index, list)</tt>. If <b>list</b> is a JavaScript object, <b>iterator</b>'s
arguments will be <tt>(value, key, list)</tt>. If the <b>list</b> has an <b>each</b>
method of its own, it will be used instead. Delegates to the native
<b>forEach</b> function if it exists.
</p>

View File

@@ -26,7 +26,7 @@ $(document).ready(function() {
answers = [];
var obj = {one : 1, two : 2, three : 3};
obj.constructor.prototype.four = 4;
_.each(obj, function(pair){ answers.push(pair.key); });
_.each(obj, function(value, key){ answers.push(key); });
equals(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.');
delete obj.constructor.prototype.four;
@@ -136,7 +136,7 @@ $(document).ready(function() {
ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');
var numbers = _.toArray({one : 1, two : 2, three : 3});
equals(_.pluck(numbers, '0').join(', '), 'one, two, three', 'object flattened into array');
equals(numbers.join(', '), '1, 2, 3', 'object flattened into array');
});
test('collections: size', function() {

View File

@@ -42,12 +42,8 @@
} else if (obj.each) {
obj.each(function(value) { iterator.call(context, value, index++, obj); });
} else {
var i = 0;
for (var key in obj) if (Object.prototype.hasOwnProperty.call(obj, key)) {
var value = obj[key], pair = [key, value];
pair.key = key;
pair.value = value;
iterator.call(context, pair, i++, obj);
iterator.call(context, obj[key], key, obj);
}
}
} catch(e) {
@@ -61,8 +57,8 @@
_.map = function(obj, iterator, context) {
if (obj && obj.map) return obj.map(iterator, context);
var results = [];
_.each(obj, function(value, index) {
results.push(iterator.call(context, value, index));
_.each(obj, function(value, index, list) {
results.push(iterator.call(context, value, index, list));
});
return results;
};
@@ -137,8 +133,8 @@
_.include = function(obj, target) {
if (_.isArray(obj)) return _.indexOf(obj, target) != -1;
var found = false;
_.each(obj, function(pair) {
if (found = pair.value === target) {
_.each(obj, function(value) {
if (found = value === target) {
throw '__break__';
}
});
@@ -361,12 +357,18 @@
// Retrieve the names of an object's properties.
_.keys = function(obj) {
return _.pluck(obj, 'key');
return _.reduce(obj, [], function(memo, value, key) {
memo.push(key);
return memo;
});
};
// Retrieve the values of an object's properties.
_.values = function(obj) {
return _.pluck(obj, 'value');
return _.reduce(obj, [], function(memo, value) {
memo.push(value);
return memo;
});
};
// Extend a given object with all of the properties in a source object.