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
_.chain & _.each are not playing together? #1391
Comments
That's correct. Unlike |
ok so map, how about _.chain().tap(function(values) { values.forEach(function(current) {}; }); |
Yep, looks good by me: _.chain([1,2,3,4])
.tap(_.bind(console.log, console))
.map(function(val) { return val * 2 })
.value(); // [2,4,6,8] |
It would be nice to mention that each doesn't return a value in the documentation. |
@xixixao |
I don't see anything preventing Underscore from making |
Why would each need to be chainable? You're not mutating the array in any way (the way you are with something like a map or filter), so therefore there is no no expectations of the value being reusable. |
Why not allow chaining, what could it hurt? It's handy for cases like |
I would make the method undefined in a wrapped object context. |
@Dw40 but what are you doing in those intermediate actions that can't better be handled with a map? |
It doesn't really matter what the intermediate step is, it could be a number of things, the benefit of |
@Dw40 want to whip up a quick pull? You're talking about special-casing |
@akre54 I know that That said, I agree that each should just return the original collection for chaining (always, whatever syntax is |
Simpler than that. Just have |
But then you're breaking compat with the spec. That's not what |
I previously wrote:
I don't see that as a big deal. Underscore already parts with the spec'ed behavior for better dev usability in several methods. For example in edge |
I don't think it's a huge change, but it's definitely unexpected that To be clear, you're talking about making this change, right? diff --git a/underscore.js b/underscore.js
index 7a30b0a..2b56536 100644
--- a/underscore.js
+++ b/underscore.js
@@ -74,7 +74,7 @@
// Handles objects with the built-in `forEach`, arrays, and raw objects.
// Delegates to **ECMAScript 5**'s native `forEach` if available.
var each = _.each = _.forEach = function(obj, iterator, context) {
- if (obj == null) return;
+ if (obj == null) return obj;
if (nativeForEach && obj.forEach === nativeForEach) {
obj.forEach(iterator, context);
} else if (obj.length === +obj.length) {
@@ -87,6 +87,7 @@
if (iterator.call(context, obj[keys[i]], keys[i], obj) === breaker) return;
}
}
+ return obj;
}; |
Right. |
I dont particularly care one way or another but it would be good to hear from more people about real use cases for or against this before merging. |
I think you can go for it. Would be a little more useful. |
In my opinion at least when working in a chained context, making _.each return a value is expected behavior, so +1 :) |
@LeonFedotov @Dw40 give that a try. |
@akre54 Awesome. |
@akre54 great, thanks! |
Interestingly, we've reverted to the pre-1.1.3 behaviour. The following addition to the 1.1.3 release notes was made in 3b916a2:
|
Ha. I knew it seemed familiar. Good spot.
|
Is there now an alternative method that can be chained and allow modifying elements of an array in place? I was searching for something like a
|
Hi I'm not sure but I think this is a bug:
I guess chained function have to return values but then tap is not an iterable, im looking for a way to access the values without un-chaining them and without mutation.
The text was updated successfully, but these errors were encountered: