简体   繁体   English

Array.splice不会更改长度

[英]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 我有一个函数,可以使用.splice删除数组的某些部分,但是当我查看console.log的长度时,它并没有改变..使用hexdump时,我还看到“已删除”字符串仍然存在

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") (应删除的值来自另一个变量,这就是为什么我用“ 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 数独是3D矩阵:第一个D和第二个D是x和y行/列,而第三个Dimension用于其余位置

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... 因为我有一个IF需要知道我的数组的长度...

after I threw a bunch of more console.log into my code I also saw that stuff... Sometimes... 在我的代码中添加了更多的console.log之后,我也看到了这些东西...有时...

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... 它可以在jsfiddle中工作,但无法在我的真实代码中解决我的问题...有时我在我的代码中看到“未定义” ..但更大的问题是它也将hexdumps保留在那里...因此,即使我直接说他变小了,也没有改变...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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