Skip to main content

Bug Tracker

Side navigation

#7108 closed bug (invalid)

Opened October 01, 2010 05:22PM UTC

Closed October 01, 2010 07:29PM UTC

Last modified October 01, 2010 07:32PM UTC

slice() end count error

Reported by: pjobson Owned by:
Priority: undecided Milestone: 1.4.3
Component: unfiled Version: 1.4.2
Keywords: Cc:
Blocked by: Blocking:
Description

This is kind of particularity I found when working with negative indexes and slice. I'm not 100% sure if it is a bug or expected behavior.

I'll just get onto the example:

<ul>

<li>0</li>

<li>1</li>

<li>2</li>

<li>3</li>

<li>4</li>

<li>5</li>

<li>6</li>

<li>7</li>

<li>8</li>

<li>9</li>

<li>10</li>

<li>11</li>

<li>12</li>

<li>13</li>

<li>14</li>

<li>15</li>

<li>16</li>

<li>17</li>

<li>18</li>

<li>19</li>

</ul>

<script type="text/javascript">

var x = [-20,-19,-18,-17,-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19];

$.each(x,function(a,b) {

console.log(b,$('li').slice(b,b+1))

});

console.log($('li').slice(-1,0));

console.log($('li').slice(-1));

</script>

When variable b gets to -1, nothing is returned, though I expect what should be returned is the li at the 19th position.

The final two console.log examples show it failing then succeeding, the main concern is if the second variable must be something (not null or ''), it makes it kind of a pain in the butt to do dynamic list indexing, as I would have to do some kludge like the following:

var x;

if (b==-1) {

x = $('li').slice(b);

} else {

x = $('li').slice(b,b+1);

}

Attachments (0)
Change History (2)

Changed October 01, 2010 07:29PM UTC by snover comment:1

resolution: → invalid
status: newclosed

Thanks for the report, but this is not a jQuery bug. jQuery simply inherits the browser’s native Array.slice functionality. You will need to use a conditional as you suggested at the end of the bug description. test case

Changed October 01, 2010 07:32PM UTC by snover comment:2