我正在尝试创建一个JavaScript函数,它将迭代表示嵌套数组中的1个或多个形状的数组,并且每个这些嵌套数组可能有不同数量的坐标来创建这些形状。在下面,我已经开始了此函数的开头,但是在思考逻辑时遇到了困难。
例如:
例如:
//This is a single 'shape' with 3 points
var coordsingle = [[1,1],[2,2],[3,3]];
//This one has 3 'shapes'
var coordmultiple = [[[1,1],[2,2],[3,3]],[[4,4],[5,5],[6,6],[7,7],[8,8]],[[-1,-1], [-2,-2],[-3,-3],[-4,-2],[-7,-3]]];
输出结果:
coordsingle = 1形状
1 - 坐标 [1,1],[2,2],[3,3]
coordmultiple = 3形状
1 - 坐标 [1,1],[2,2],[3,3]
2 - 坐标 [4,4],[5,5],[6,6],[7,7],[8,8]
3 - 坐标 [-1,-1], [-2,-2],[-3,-3],[-4,-2],[-7,-3]
function extractCoordinates(shapeArray){
let shapecount = data.length;
if(shapecount < 2){
document.write("<br>"," The Shape count is ",shapecount,"<br>");
document.write("<br>"," The amount of Coords ",data[0][0].length,"<br>");
document.write("<br>",data,"<br>");
}
else{
// this only outputs the first nested arrays set of coordinates
for(var coordpairs of shapecount){
document.write("<br>"," The Shape count is ",shapecount,"<br>");
document.write("<br>"," The amount of Coords ",data[0][0].length,"<br>");
document.write("<br>",data,"<br>");
}
}