Skip to main content

Bug Tracker

Side navigation

#12003 closed bug (invalid)

Opened July 02, 2012 08:54PM UTC

Closed July 02, 2012 08:56PM UTC

Last modified July 02, 2012 10:57PM UTC

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

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

var a = new Array(4);

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

This should return 0, shouldn't it?

Attachments (0)
Change History (4)

Changed July 02, 2012 08:56PM UTC by scottgonzalez comment:1

resolution: → invalid
status: newclosed

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

Changed July 02, 2012 09:08PM UTC by anonymous comment:2

@reporter

var a = [4];

console.log($.inArray(4, a)); 0

var a = [1,4];

console.log($.inArray(4, a)); 1

Changed July 02, 2012 09:17PM UTC by rwaldron comment:3

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

Changed July 02, 2012 10:57PM UTC by dmethvin comment:4

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