Bug Tracker

Modify

Ticket #12003 (closed bug: invalid)

Opened 11 months ago

Last modified 11 months ago

jQuery.inArray() returns -1 for value that is in array if it is the only element in array

Reported by: vic.emond@… Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.7.2
Keywords: Cc:
Blocking: Blocked by:

Description

var a = new Array(4);

$.inArray(4, a) returns -1.

This should return 0, shouldn't it?

Change History

comment:1 Changed 11 months ago by scott.gonzalez

  • Status changed from new to closed
  • Resolution set to invalid

new Array(4) create [,,,] not [4].

comment:2 Changed 11 months ago by anonymous

@reporter

var a = [4]; console.log($.inArray(4, a)); 0

var a = [1,4]; console.log($.inArray(4, a)); 1

comment:3 Changed 11 months ago by rwaldron

Yes, here you've used a literal. In your original example, you used the new Array() constructor, which creates arrays like...

var a = new Array(1);

a.length; // 1
a[0]; // undefined


var a = new Array(1, 2, 3);

a.length; // 3
a[0]; // 1

The Array constructor creates an array of N length when passed a single numeric argument

comment:4 Changed 11 months ago by dmethvin

alert(new Array(3).join("ha"));

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.