Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_.each fumbles when object has a length property #659

Closed
rajeshsegu opened this issue Jun 28, 2012 · 2 comments
Closed

_.each fumbles when object has a length property #659

rajeshsegu opened this issue Jun 28, 2012 · 2 comments

Comments

@rajeshsegu
Copy link

For example, consider this server response:

response : {
length: 100;
resultsA: [a, b, c, d],
resultsB: [x, y, z]
......
}

_.each(response, function(key, value){
if(key == "length") return;
//otherwise, process results
});

This would fail to do forEach as length attribute would not let _.each to iterate as an Object iterator. In addition, there are 100 void iterations that happen before its returned.

I understand its is a bad practice to have length attribute as part of an Object but I assume underscore framework is designed to be prepared for the worst case scenario too.

Fix:

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) { //additional check to check if obj is an array. typeof ?
for (var i = 0, l = obj.length; i < l; i++) {
if (i in obj && 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;
}
}
}
};

@michaelficarra
Copy link
Collaborator

Please search next time. See #448, #297, #252, #148.

@jdalton
Copy link
Contributor

jdalton commented Jun 28, 2012

I've handled this in libs I use by providing something like forOwn and forIn methods. That way it gives devs a bit more control over object iteration and avoids problems with overloading forEach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants