Bug Tracker

Opened 14 years ago

Closed 14 years ago

Last modified 11 years ago

#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
Blocked by: Blocking:


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 (1)

jquery_inarray.patch (1.2 KB) - added by gabriel 14 years ago.

Download all attachments as: .zip

Change History (3)

Changed 14 years ago by gabriel

Attachment: jquery_inarray.patch added

comment:1 Changed 14 years ago by flesler

Cc: gabriel added
Milestone: 1.3
Resolution: invalid
Status: newclosed

That behavior is something else. We just imitate indexOf.

Adding that could be unexpected for some and will add overhead.

So no, this won't be changed.

comment:2 Changed 11 years ago by [email protected]

Dang! I need exactly this!

Wasn't this added as a new function, if mr angel Gabriel decided to reject it based on it being unexpected (duh, behaviour seems pretty logical to me).

Note: See TracTickets for help on using tickets.