简体   繁体   中英

Array.splice doesn't change the length

I have a function where I delete some parts of an Array using .splice , but when I look about console.log on it's length it hasn't changed.. and with hexdump I also saw that the "deleted" string is still there

eg

Sudoku[j][y] = [3, 7]
Sudoku[x][y][k] = 3

function...
Sudoku[j][y].splice(Sudoku[j][y].indexOf(Sudoku[x][y][k]), 1)

console.log(Sudoku[j][y], Sudoku[j][y].length, Hexdump.dump(Sudoku[j][y]))
= [7] 2 /*Zusammenfassung des hex:*/ 3, 7

(the value that shall be deleted comes from an other var, that's why I wrote the part with the "indexOf")

The Sudoku is a 3D Matrix: the first D ans second D are the x and y rows/columns, while the third Dimension is for the rest posibilities

What can I do, to delete the value once and for all?

because I have an IF that needs to know the length of my Arrays...

after I threw a bunch of more console.log into my code I also saw that stuff... Sometimes...

console.log(sudoku[j][y].length, sudoku[j][y], sudoku[j][y].indexOf(sudoku[x][y][k]))
sudoku[j][y].splice(sudoku[j][y].indexOf(sudoku[x][y][k]), 1);
console.log(sudoku[j][y].length, sudoku[j][y])

Results into:

4 [7, 9] 0
3 [7, 9]

so my newest try was to use an new method instead of splice:

sudoku[x][j][(sudoku[x][j].indexOf(sudoku[x][y][k]))]=sudoku[x][j][sudoku[x][j][sudoku[x][y].length-1]]
sudoku[x][j].length--

It worked in jsfiddle but ain't solved my problems in my real code... sometimes I saw a "undefined" in my code.. but the bigger problem was that it also left the hexdumps there... so that the lenght, even after I directly said him to get smaller, hasn't changed...

You're splicing an element that is outside of the array:

var arr = [1,2,3];
arr.splice(3,1);//doesn't take anything out of the array
arr.length===3;//true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM