我想从数组中移除一些元素,并使用splice功能同时返回这些值。请参见下面的javascript代码:
当尝试运行这一行代码(与整个函数一起),会出现以下错误:
该数组是通过对另一个数组进行洗牌操作创建的(shuffle函数不是我写的):
我尝试记录所有数字,这一步骤已经正确地完成。数组正确地进行了洗牌。然而,在对其进行splice时,会出现相同的错误。请注意,我是在for循环内执行splice操作的。以下是代码。
var blocksUsed = allNumbers.splice(0,3);
当尝试运行这一行代码(与整个函数一起),会出现以下错误:
我花了相当长的时间检查函数中是否存在排版错误,但似乎没有发现任何问题。我还发现JQuery经常是造成此类问题的罪魁祸首,但我并未使用JQuery。Uncaught TypeError: undefined is not a function
该数组是通过对另一个数组进行洗牌操作创建的(shuffle函数不是我写的):
function shuffle(o){
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
然后我会用以下方式使用它:
var allNumbers = shuffle(blocks);
我尝试记录所有数字,这一步骤已经正确地完成。数组正确地进行了洗牌。然而,在对其进行splice时,会出现相同的错误。请注意,我是在for循环内执行splice操作的。以下是代码。
for (var i = 0; i < 4; i++){
// Get a block of 3 blocks.
var blocksUsed = allNumbers.splice(0,3); // This line gives the error.
// Determine the color used for these blocks and remove it so that it doesn't get drawn again.
var colorIndex = randomIntFromInterval(0,i);
var colorUsed = otherColors[colorIndex];
if(otherColors.indexOf(colorUsed) > -1){
otherColors.splice(colorIndex, 1);
}
// For each block, set the color.
for(var j = 0; j < 3; j++){
blocksUsed[j].className = "block " + colorUsed;
}
}
为什么我不能在这个数组上使用splice
?如果需要,我可以提供整个函数。您可以参考codepen。
因为有以下注释:
allNumbers
是一个打乱顺序的数组,包含按以下方式接收到的元素:
var blocks = document.getElementsByClassName('block');
blocks
不是一个数组,它是一个NodeList
。allNumbers
也是如此。 - Barmar