Closed Bug 212581 Opened 23 years ago Closed 22 years ago

Array splice() function not truncating array when deleting

Categories

(Rhino Graveyard :: Core, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED WORKSFORME

People

(Reporter: sgutz, Assigned: norrisboyd)

Details

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) Build Identifier: The Array class splice() function doesn't always truncate the array when removing an element. For example: myArray.splice( 27, 1 ); The correct element is removed, but the last element in the array appears twice rather then shortening the array by one element. Reproducible: Always Steps to Reproduce: 1. 2. 3.
cc'ing Igor, but I can't reproduce the bug with the following example: $ java org.mozilla.javascript.tools.shell.Main Rhino 1.5 release 5 0000 00 00 js> js> arr = []; js> for (var i=0; i<31; i++) {arr[i] = i;} js> arr 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30 js> arr.length 31 js> var removed = arr.splice(27,1) js> removed 27 js> arr 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,28,29,30 js> arr.length 30 Steven: can you give the complete test you are using? And what version of Rhino are you using? Thanks -
I can't give you the exact code because it is buried within a proprietary application, but I have an array or strings created from code similar to the following: var myArray = new Array(); myString = "string1,string2,string3,string4,string5"; myArray = myString.split( "," ); myArray.sort(); . . . lengthb4 = myArray.length; myArray.splice( 2, 0 ); lengthAfter = myArray.length; At this point the desired item has been deleted from the array, but the array length is still that same (lengthB4 == lengthAfter), and the last element is now duplicated. Though sometimes it seems to work properly (it seems to depend on which element gets deleted) To work around the problem I create a new Array and copy all but the last element to (assuming the array length before and after the splice is the same). I can't give you much more help than that - maybe if I have time I'll trace through the Rhino code myself to see if I can track it down for you. It Rhino 1.5R4_1 BTW.
Your original report mentioned |myArray.splice( 27, 1 )|. The above comment mentions |myArray.splice(2, 0)|. There is a big difference between 1 and 0 as the second parameter! Don't have time to look this up in the spec right now, but here is the syntax for splice() from http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/array.html --- SYNTAX splice(index, howMany, [element1][, ..., elementN]) PARAMETERS index Index at which to start changing the array. howMany An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed. In this case, you should specify at least one new element. element1, ..., elementN The elements to add to the array. If you don't specify any elements, splice simply removes elements from the array. --- Note in particular the line > If howMany is 0, no elements are removed. In this case, you should > specify at least one new element. Is what you are seeing inconsistent with this specification?
Sorry, it should actually be a 1 not a 0 myArray.splice( 2, 1 );
Can not reproduce the bug. Steven, please reopen it if you have a reproducible standalone example or any other information that can help to find out why splice did not work for you.
Status: NEW → RESOLVED
Closed: 22 years ago
Resolution: --- → WORKSFORME
Trageting as resolved against 1.5R5
Target Milestone: --- → 1.5R5
Sorry but even with Rhino 1.7R4, this (serious) bug is not fixed ! Ex: var tab = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29]; var l = tab.length; var r = Math.floor(Math.random() * l); var val = tab.splice(r, 1); my tab becomes [0,1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,29]
You need to log in before you can comment on or make changes to this bug.