Ticket #3687 (closed bug: invalid)
jQuery.inArray should not return -1 when matching nested arrays
| Reported by: | gabriel | Owned by: | |
|---|---|---|---|
| Priority: | major | Milestone: | 1.3 |
| Component: | core | Version: | 1.2.6 |
| Keywords: | inArray, arrays, utility | Cc: | gabriel |
| Blocking: | Blocked by: |
Description
I'm not not sure why this is, but when comparing two identical Arrays, the JavaScript engine returns false, even though their elements are exactly the same.
So if I'm trying to find if an array is a member of a dataset, jQuery.inArray will always return -1, which is not the right behaviour.
I've written a patch that solves this problem. With my patch, the following works:
var a = [1, 2]; var b = ["x", true, 7, a]; var c = [[0, 1], [1, 2], [2, 3]]; jQuery.inArray(a, b); //=> returns 3 jQuery.inArray(a, c); //=> returns 1
This patch also includes a new public utility method, jQuery.compareArrays, which can be used to check if two arrays are the same. I opted for making this a publicly-accessible method since it could be useful to others in their code and not just within jQuery.
Also, if there's a better way of checking if a variable is an Array (without having to write a whole bunch of extra code), please feel free to amend my patch. :)
Attachments
Change History
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

