JS returns 32 元素数组中只有 3 个元素
JS returns only 3 elements from 32 element array
我正在尝试让棋盘上出现的所有棋子。数组似乎已成功填满 32 个(起始位置有效)。但是,在 switch 比较中,它只找到其中的 3 个。我正在使用 img src 参数来区分各个部分。
JS 代码:
function SetupPieces()
{
var piecesObj = document.getElementsByClassName("chess_com_piece chess_com_draggable");
let imgSrc = [];
for(var i=0;i<piecesObj.length;i++)
{
imgSrc[i] = piecesObj[i].src.substring(60);
}
var Pieces = [], FirstPosition = [], SecondPosition = [], BRookX,BRookY, WRookX,WRookY,BRook,WRook;
for(var i=0;i<imgSrc.length;i++)
{
var TempPieceLocObj = (piecesObj[i].style.transform).substring(10);
TempPieceLocObj = (TempPieceLocObj.substring(0,TempPieceLocObj.length - 1)).split(",");
FirstPosition[i] = (TempPieceLocObj[0].split("px"))[0];
SecondPosition[i] = (TempPieceLocObj[1].split("px"))[0];
console.log("Array size - " + imgSrc.length);
switch(imgSrc[i])
{
case "wp.png":
Pieces[i] = "White Pawn";
break;
case "bp.png":
Pieces[i] = "Black Pawn";
break;
case "br.png":
Pieces[i] = "Black Rook";
BRook = (piecesObj[i].style.transform).substring(10);
BRook = (BRook.substring(0,BRook.length - 1)).split(",");
BRookY = parseInt(BRook[1].substring(0,BRook[1].length - 3),10) * 1.029684601113173;
BRookX = parseInt(BRook[0].substring(0,BRook[1].length - 1),10) * 1.029684601113173;
break;
case "wr.png":
Pieces[i] = "White Rook";
WRook = (piecesObj[i].style.transform).substring(10);
WRook = (WRook.substring(0,WRook.length - 1)).split(",");
WRookY = parseInt(WRook[1].substring(0,WRook[1].length - 1),10) * 1.029684601113173;
WRookX = parseInt(WRook[0].substring(0,WRook[1].length - 3),10) * 1.029684601113173;
break;
case "bn.png":
Pieces[i] = "Black Knight";
break;
case "wn.png":
Pieces[i] = "White Knight";
break;
case "bb.png":
Pieces[i] = "Black Bishop";
break;
case "wb.png":
Pieces[i] = "White Bishop";
break;
case "wq.png":
Pieces[i] = "White Queen";
break;
case "bq.png":
Pieces[i] = "Black Queen";
break;
case "bk.png":
Pieces[i] = "Black King";
break;
case "wk.png":
Pieces[i] = "White King";
break;
}
}
if(WRookX !== 0) {BoardSize = WRookX;}
return [FirstPosition,SecondPosition,Pieces];
}
控制台输出 - https://prnt.sc/uhrg79
imgSrc 数组中的片段 - https://prnt.sc/uhrgxz
我正在使用这个简单的代码输出它们:
var testing = SetupPieces();
for(var i = 0; i<testing.length;i++)
{
console.log("X:" + testing[0][i] + " Y:" + testing[1][i] + " Name:" + testing[2][i]);
}
testing
将等于函数 ([FirstPosition,SecondPosition,Pieces]
) 的 return,因此
testing.length
将是 3。
您可能是指 testing[2].length
,它们是 pieces
?
我正在尝试让棋盘上出现的所有棋子。数组似乎已成功填满 32 个(起始位置有效)。但是,在 switch 比较中,它只找到其中的 3 个。我正在使用 img src 参数来区分各个部分。 JS 代码:
function SetupPieces()
{
var piecesObj = document.getElementsByClassName("chess_com_piece chess_com_draggable");
let imgSrc = [];
for(var i=0;i<piecesObj.length;i++)
{
imgSrc[i] = piecesObj[i].src.substring(60);
}
var Pieces = [], FirstPosition = [], SecondPosition = [], BRookX,BRookY, WRookX,WRookY,BRook,WRook;
for(var i=0;i<imgSrc.length;i++)
{
var TempPieceLocObj = (piecesObj[i].style.transform).substring(10);
TempPieceLocObj = (TempPieceLocObj.substring(0,TempPieceLocObj.length - 1)).split(",");
FirstPosition[i] = (TempPieceLocObj[0].split("px"))[0];
SecondPosition[i] = (TempPieceLocObj[1].split("px"))[0];
console.log("Array size - " + imgSrc.length);
switch(imgSrc[i])
{
case "wp.png":
Pieces[i] = "White Pawn";
break;
case "bp.png":
Pieces[i] = "Black Pawn";
break;
case "br.png":
Pieces[i] = "Black Rook";
BRook = (piecesObj[i].style.transform).substring(10);
BRook = (BRook.substring(0,BRook.length - 1)).split(",");
BRookY = parseInt(BRook[1].substring(0,BRook[1].length - 3),10) * 1.029684601113173;
BRookX = parseInt(BRook[0].substring(0,BRook[1].length - 1),10) * 1.029684601113173;
break;
case "wr.png":
Pieces[i] = "White Rook";
WRook = (piecesObj[i].style.transform).substring(10);
WRook = (WRook.substring(0,WRook.length - 1)).split(",");
WRookY = parseInt(WRook[1].substring(0,WRook[1].length - 1),10) * 1.029684601113173;
WRookX = parseInt(WRook[0].substring(0,WRook[1].length - 3),10) * 1.029684601113173;
break;
case "bn.png":
Pieces[i] = "Black Knight";
break;
case "wn.png":
Pieces[i] = "White Knight";
break;
case "bb.png":
Pieces[i] = "Black Bishop";
break;
case "wb.png":
Pieces[i] = "White Bishop";
break;
case "wq.png":
Pieces[i] = "White Queen";
break;
case "bq.png":
Pieces[i] = "Black Queen";
break;
case "bk.png":
Pieces[i] = "Black King";
break;
case "wk.png":
Pieces[i] = "White King";
break;
}
}
if(WRookX !== 0) {BoardSize = WRookX;}
return [FirstPosition,SecondPosition,Pieces];
}
控制台输出 - https://prnt.sc/uhrg79
imgSrc 数组中的片段 - https://prnt.sc/uhrgxz
我正在使用这个简单的代码输出它们:
var testing = SetupPieces();
for(var i = 0; i<testing.length;i++)
{
console.log("X:" + testing[0][i] + " Y:" + testing[1][i] + " Name:" + testing[2][i]);
}
testing
将等于函数 ([FirstPosition,SecondPosition,Pieces]
) 的 return,因此
testing.length
将是 3。
您可能是指 testing[2].length
,它们是 pieces
?