假设我有一个对象数组,例如
let strokeArray = [
{
id:1234,
data: [
[101,122],
[105,124],
[107,127],
[109,129],
]
},
{
id:5678,
data: [
[201,522],
[205,524],
[207,527],
[209,529],
]
},
{
id:0001,
data: [
[601,622],
[205,595],
[607,627],
[609,629],
]
}
]
Run Code Online (Sandbox Code Playgroud)
我有很多要点[x,y]要说[202,523]。这个需要和数据对比
我想找到strokeArray中的哪个数组元素包含最接近上述点的值。
在上述情况下,答案是 strokeArray[1] 因为它具有数据中最近的坐标。
我还需要一些边界条件来搜索。
[300,600]应该返回空,因为它离任何数据都不远。[205,595]必须返回strokeArray[2]。因为它有精确匹配。我试过的是
strokeArray.forEach((stroke) => {
stroke.data.forEach((coordinate, index) => {
if(coordinate === [205,595]){return index}
// But this only checks exach match
});
});
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我实现这一目标。