-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
_.indexOf doesn't work on arrays of objects #557
Comments
The change to |
Can you clarify why you chose to rewrite |
I did make a mistake in my proposed code. Doing the deep equality check would mean you couldn't use the native I also made a mistake in naming the issue! I discovered this when trying to use
Passing the flag through all the methods that use |
Modified the original post to reflect this. |
JavaScript uses object identity equality, not deep equality. Underscore follows suit. |
I agree with you that object identity equality should be the default! I was at least hoping for a flag one could set that would use |
+1 for suggestion made by @zetlen, deep Equality comparison of objects within an array would be very useful. |
+1 |
Best not to extend the semantics of _.mixin({
'deepIndex': function (array, item) {
var result = -1;
_.some(array, function (value, index) {
if (_.isEqual(value, item)) {
result = index;
return true;
}
});
return result;
}
}); |
Thanks, I basically did just that. |
If we are going to do everything the way JavaScript does by default, why not just delete the underscore.js library then? |
Really bravadomizzou? It provides fallbacks for browsers that don't support it and provides some extra functionality that default javascript doesn't have. |
I wonder why the following two samples work differently: var a = [{23:'hello'},{283:'goodbye'}] var c = .findWhere(a, {283:'goodbye'}); I have created a fiddle here: http://jsfiddle.net/rjcXL/1/ |
@tapsboy It's the same reason ({a: 1}) === ({a: 1}); is ({a: 1}).a === ({a: 1}).a; is |
I encountered the same issue, I used _.findWhere instead of _.indexOf. Thanks. |
_.indexOf
doesn't use_.isEqual
for its test, so you can't look up the index of an object in an array.var a = [{23:'hello'},{283:'goodbye'}],
b = {283:'goodbye'};
I propose the following change to
_.indexOf
.old version
New version uses an optional deepEquality flag. Deep equality test cannot use nativeIndexOf and is significantly slower.
The text was updated successfully, but these errors were encountered: