mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 10:27:49 +00:00
Issue #2 -- each now calls iterator with (value, key, list) when iterating over javascript objects
This commit is contained in:
@@ -171,10 +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. Each invocation of <b>iterator</b> is called with three arguments,
|
passed. Each invocation of <b>iterator</b> is called with three arguments:
|
||||||
<i>element</i>, <i>index</i>, and the <b>list</b>.
|
<tt>(element, index, list)</tt>. If <b>list</b> is a JavaScript object, <b>iterator</b>'s
|
||||||
If <b>list</b> is a JavaScript object, a pair with <b>key</b>
|
arguments will be <tt>(value, key, list)</tt>. If the <b>list</b> 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.
|
||||||
</p>
|
</p>
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ $(document).ready(function() {
|
|||||||
answers = [];
|
answers = [];
|
||||||
var obj = {one : 1, two : 2, three : 3};
|
var obj = {one : 1, two : 2, three : 3};
|
||||||
obj.constructor.prototype.four = 4;
|
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.');
|
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;
|
||||||
|
|
||||||
@@ -136,7 +136,7 @@ $(document).ready(function() {
|
|||||||
ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');
|
ok(_.isArray(_.toArray(arguments)), 'arguments object converted into array');
|
||||||
|
|
||||||
var numbers = _.toArray({one : 1, two : 2, three : 3});
|
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() {
|
test('collections: size', function() {
|
||||||
|
|||||||
@@ -42,12 +42,8 @@
|
|||||||
} else if (obj.each) {
|
} else if (obj.each) {
|
||||||
obj.each(function(value) { iterator.call(context, value, index++, obj); });
|
obj.each(function(value) { iterator.call(context, value, index++, obj); });
|
||||||
} else {
|
} else {
|
||||||
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];
|
iterator.call(context, obj[key], key, obj);
|
||||||
pair.key = key;
|
|
||||||
pair.value = value;
|
|
||||||
iterator.call(context, pair, i++, obj);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
@@ -61,8 +57,8 @@
|
|||||||
_.map = function(obj, iterator, context) {
|
_.map = function(obj, iterator, context) {
|
||||||
if (obj && obj.map) return obj.map(iterator, context);
|
if (obj && obj.map) return obj.map(iterator, context);
|
||||||
var results = [];
|
var results = [];
|
||||||
_.each(obj, function(value, index) {
|
_.each(obj, function(value, index, list) {
|
||||||
results.push(iterator.call(context, value, index));
|
results.push(iterator.call(context, value, index, list));
|
||||||
});
|
});
|
||||||
return results;
|
return results;
|
||||||
};
|
};
|
||||||
@@ -137,8 +133,8 @@
|
|||||||
_.include = function(obj, target) {
|
_.include = function(obj, target) {
|
||||||
if (_.isArray(obj)) return _.indexOf(obj, target) != -1;
|
if (_.isArray(obj)) return _.indexOf(obj, target) != -1;
|
||||||
var found = false;
|
var found = false;
|
||||||
_.each(obj, function(pair) {
|
_.each(obj, function(value) {
|
||||||
if (found = pair.value === target) {
|
if (found = value === target) {
|
||||||
throw '__break__';
|
throw '__break__';
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -361,12 +357,18 @@
|
|||||||
|
|
||||||
// Retrieve the names of an object's properties.
|
// Retrieve the names of an object's properties.
|
||||||
_.keys = function(obj) {
|
_.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.
|
// Retrieve the values of an object's properties.
|
||||||
_.values = function(obj) {
|
_.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.
|
// Extend a given object with all of the properties in a source object.
|
||||||
|
|||||||
Reference in New Issue
Block a user