[英]Javascript array inside of another array (multidimensional)
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?!
想不出来?!
You can use slice
to get part of an array, for example 例如,您可以使用
slice
来获取数组的一部分
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
, ie [cells[0], cells[1], cells[2]]
. 呼叫
cells.slice(0, 3)
返回与的元素的数组cells
从指数开始0
至多且不含 3
,即[cells[0], cells[1], cells[2]]
You can wrap another array over that "manually" to get the desired result. 您可以在“手动”上包装另一个数组以获得所需的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.