Bug Tracker

Modify

Ticket #3687 (closed bug: invalid)

Opened 5 years ago

Last modified 2 years ago

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

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

Change History

Changed 5 years ago by gabriel

comment:1 Changed 5 years ago by flesler

  • Cc gabriel added
  • Status changed from new to closed
  • Resolution set to invalid
  • Milestone set to 1.3

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 2 years ago by bartvanderwal@…

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

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.