0

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

e.g.

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...

Robin93K
  • 198
  • 2
  • 3
  • 15
  • Works [here](http://jsfiddle.net/Badye/). What's `Hexdump`? Hmm... You've changed your array from 1D to 2D... – Teemu Aug 19 '13 at 12:12
  • Actually it is 3D now and I think there is a typo at `Array.indexOf(3)`... – user1983983 Aug 19 '13 at 12:17
  • I changed it, like it's in my code... – Robin93K Aug 19 '13 at 12:17
  • [link](http://jsfiddle.net/Badye/4/embedded/result/) Isn't working when I write my code.. – Robin93K Aug 19 '13 at 12:23
  • hexdump is to show a value as hexcode, coded and decodet it says that Sudoku[j][y] is still 3,7 – Robin93K Aug 19 '13 at 12:25
  • When you link to jsFiddle, use a link like http://jsfiddle.net/Badye/4/ , i.e. the path up to version number only. – Teemu Aug 19 '13 at 12:46
  • Don't understand, how can the lenght of the array be 4 but it logs only 2 elements. What browser are you using for that? – HMR Aug 19 '13 at 14:07
  • I'm using Firefox 21.0... And I don't completly underastand it either.. but it's like splice ain't removing the values, it just makes them invisible. Until you use hexdump, than you can see them again... – Robin93K Aug 19 '13 at 14:21

1 Answers1

0

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
HMR
  • 37,593
  • 24
  • 91
  • 160
  • Sudoku[j][y].indexOf(Sudoku[x][y][k]) = 0, so it's Sudoku[j][y].splice(0, 1), what is definitly inside – Robin93K Aug 19 '13 at 12:41
  • @Robin93K Are you sure? Did you console.log it? The only way I can get a splice to do nothing is when I use a number equal to or higher than the array's length. – HMR Aug 19 '13 at 12:53