另一个数组里面的Javascript数组(多维)

时间:2021-12-02 01:23:07

I have an array

我有一个阵列

cells = [0, 0, 0, 0, 0, 0, 0, 0, 0];

which is updated with a jQuery function

这是用jQuery函数更新的

 $(document).ready(function(){
...
    $('#box').click(function(e){

        var indexArray = e.target.id.split('_');

        if (indexArray.length > 1) {
            var index = indexArray[1];

            if (cells[index] == 0){
                cells[index] = move;
...
})

I want to make a cross-check of cells array groups. for example:

我想对单元格数组进行交叉检查。例如:

(cells[0] + cells[1] + cells[2]);   // row 1
(cells[3] + cells[4] + cells[5]);   // row 2
(cells[6] + cells[7] + cells[8]);   // row 3
...

I tried to create a multidimensional array, but all I get is undefined:

我试图创建一个多维数组,但我得到的是未定义的:

var triggers = [[cells[0], cells[1], cells[2]]];

is it possible to pass cells arrays' variables to triggers array? Can't figure it out?!

是否可以将单元格数组的变量传递给触发器数组?想不出来?!

1 个解决方案

#1


6  

You can use slice to get part of an array, for example

例如,您可以使用切片来获取数组的一部分

var triggers = [cells.slice(0, 3)];

The call cells.slice(0, 3) returns an array with the elements of cells starting from index 0 up to and excluding 3, i.e. [cells[0], cells[1], cells[2]]. You can wrap another array over that "manually" to get the desired result.

调用cells.slice(0,3)返回一个数组,其中元素的元素从索引0开始直到3和3,即[cells [0],cells [1],cells [2]]。您可以在“手动”上包装另一个数组以获得所需的结果。

#1


6  

You can use slice to get part of an array, for example

例如,您可以使用切片来获取数组的一部分

var triggers = [cells.slice(0, 3)];

The call cells.slice(0, 3) returns an array with the elements of cells starting from index 0 up to and excluding 3, i.e. [cells[0], cells[1], cells[2]]. You can wrap another array over that "manually" to get the desired result.

调用cells.slice(0,3)返回一个数组,其中元素的元素从索引0开始直到3和3,即[cells [0],cells [1],cells [2]]。您可以在“手动”上包装另一个数组以获得所需的结果。