在以x=0 and y=0为原点、向下y-axis和向右x-axis为正轴的空间中,是否存在矩阵相对位置下变换的标准方法或算法。

[ [{x:36,y:14},{x:242,y:14}],
  [{x:36,y:133}],
  [{x:36,y:252}],
  [{x:36,y:371},{x:242,y:371},{x:446,y:371},{x:651,y:371}],
  [{x:242,y:490},{x:446,y:490},{x:651,y:490}] ]

因为这个数组的长度是5,其中最长的数组的长度是4,所以我需要用下面的格式转换5*4大小的矩阵。
[ [{x:36,y:14},{x:242,y:14},null,null],
  [{x:36,y:133},null,null,null],
  [{x:36,y:252},null,null,null],
  [{x:36,y:371},{x:242,y:371},{x:446,y:371},{x:651,y:371}],
  [null,{x:242,y:490},{x:446,y:490},{x:651,y:490}] ]

在上述情况下,将保留相对位置。
提前谢谢!啊!

最佳答案

解决方案首先将所有唯一的x值缩减为排序的平面数组。
然后在每行数据上循环,并将每行数组拼接到孔中

let data =[ [{x:36,y:14},{x:242,y:214}],
  [{x:36,y:133}],
  [{x:36,y:252}],
  [{x:36,y:371},{x:242,y:371},{x:446,y:371},{x:651,y:371}],
  [{x:242,y:490},{x:446,y:490},{x:651,y:490}] ]


let xVals  = [...new Set(data.reduce((a,c)=>a.concat(c.map(({x})=>x)),[]))].sort((a,b)=>a-b)

data.forEach(row=>{
   xVals.forEach((x,i)=>{
      if(row[i] === undefined  || row[i].x > x){
          row.splice(i,0, null)
      }
   });
});

 data.forEach(arr=>console.log(JSON.stringify(arr)))

07-24 13:04