Skip to main content

Bug Tracker

Side navigation

#3687 closed bug (invalid)

Opened December 05, 2008 06:20AM UTC

Closed December 06, 2008 12:21AM UTC

Last modified March 28, 2012 01:37PM UTC

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:
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 (1)
Change History (2)

Changed December 06, 2008 12:21AM UTC by flesler comment:1

cc: → gabriel
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.

Changed March 28, 2012 01:37PM UTC by bartvanderwal@gmail.com comment:2

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).