简体   繁体   English

使用array.splice时,Undefined不是函数

[英]Undefined is not a function when using array.splice

I am trying to remove some elements from an array, and use the splice functionality where it also returns those values. 我试图从数组中删除一些元素,并使用splice功能,它也返回这些值。 See the javascript below: 请参阅以下javascript:

var blocksUsed = allNumbers.splice(0,3);

When trying to run this line (in conjunction with the entire function), I get an error saying the following: 当试图运行这一行(与整个函数一起)时,我收到一条错误说明如下:

Uncaught TypeError: undefined is not a function 未捕获的TypeError:undefined不是函数

I have spent quite some time looking for typographical errors in the function, but there don't seem to be any. 我花了很长时间在函数中寻找印刷错误,但似乎没有。 I also ready that JQuery is the common troublemaker here, but I am not using JQuery. 我也准备好JQuery是常见的麻烦制造者,但我没有使用JQuery。

The array is created by shuffling another array (the shuffle function isn't mine): 通过改组另一个数组来创建数组(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;
};

Which I then use in the following manner: 然后我以下列方式使用它:

var allNumbers = shuffle(blocks);

I tried logging the allNumbers, which works correctly. 我尝试记录allNumbers,它正常工作。 The array gets shuffled correctly. 数组被正确洗牌。 Trying to do a splice on this however gives a same error. 然而,尝试对此进行拼接会产生相同的错误。

Also note that I am doing the splicing inside a for-loop. 还要注意我在for循环中进行拼接。 Code below. 代码如下。

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;
    }
}

Why can I not use splice on this array? 为什么我不能在这个数组上使用splice Note that I can provide the entire function, if desired. 请注意,如果需要,我可以提供整个功能。 I have the entire function at this codepen for reference straight away. 我在这个codepen上有完整的功能,可以立即参考。

Because of the comment: 由于评论:

allNumbers is a shuffled version of an array containing elements received in the following manner: allNumbers是包含以下列方式接收的元素的数组的混洗版本:

var blocks = document.getElementsByClassName('block');
var blocks = document.getElementsByClassName('block');

That is not an array. 那不是一个数组。 It is array like. 这是阵列。 You need to make it into an array. 你需要把它变成一个数组。

var elems = document.getElementsByClassName('block');
var blocks = Array.prototype.slice.call( elems, 0 );

blocks is not an Array . blocks不是Array It's a NodeList So you need to use [].slice.call which will take blocks, which is an array-like structure and return expected results. 它是一个NodeList所以你需要使用[].slice.call ,它将采用块,这是一个类似于数组的结构并返回预期的结果。

var blocksArr = [].slice.call(blocks, 0); 
var blocksUSed = blocksArr.splice(0, 3);

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

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