Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/451.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在嵌套数组javascript中查找最近的元素?_Javascript_Arrays_Ecmascript 6 - Fatal编程技术网

如何在嵌套数组javascript中查找最近的元素?

如何在嵌套数组javascript中查找最近的元素?,javascript,arrays,ecmascript-6,Javascript,Arrays,Ecmascript 6,假设我有一个对象数组,比如 let strokeArray = [ { id:1234, data: [ [101,122], [105,124], [107,127], [109,129], ] }, { id:5678, data: [ [201,522],

假设我有一个对象数组,比如

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],
        ]
    }
]
我有一个点数组
[x,y]
比如
[202523]
。这需要与数据进行比较

我想找出strokeArray中哪个数组元素包含最接近上述点的值

在上述情况下,答案是strokeArray[1],因为它在数据中有最近的坐标

我还需要一些边界条件来搜索

  • 例如,点
    [300600]
    应该返回空,因为它与任何数据都不接近
  • 如果找到精确匹配,我们需要该实例,例如
    [205595]
    必须返回strokeArray[2]。因为它完全匹配
我试过的是

strokeArray.forEach((stroke) => {
      stroke.data.forEach((coordinate, index) => {
        if(coordinate === [205,595]){return index}
        // But this only checks exach match
      });
});

有人能帮我做到这一点吗。

这是一个简单的点对点距离计算

背后的数学是

d=sqrt(A^2+B^2)
其中
d
是笛卡尔空间中点a和点B之间的距离

在2D位置的情况下,使用a(xA,yA)和B(xB,yB),穹窿变为:

d=sqrt((xA-XB)^2+(yA-yB)^2)

对于这个句子,因为它与任何数据都不接近,所以我添加了一个可配置的阈值逻辑,以便您可以丢弃远大于某个值的点

let strokeArray=[
{
身份证号码:1234,
数据:[
[101,122],
[105,124],
[107,127],
[109,129],
]
},
{
身份证号码:5678,
数据:[
[201,522],
[205,524],
[207,527],
[209,529],
]
},
{
id:0001,
数据:[
[601,622],
[205,595],
[607,627],
[609,629],
]
}
];
函数计算距离(a,b){
返回Math.sqrt(Math.pow(a[0]-b[0,2)+Math.pow(a[1]-b[1,2));
}
函数最接近笔划(笔划数组、点、阈值){
var minDistances=strokeArray.map((笔划,索引)=>{
变量距离=笔划.数据.地图((坐标)=>计算距离(坐标,点));
返回Math.min.apply(这是距离);
});
var min=Math.min.apply(这是minDistances);
返回最小值<阈值?行程数组[minDistances.indexOf(min)]:空;
}

log(nearestStroke(strokeArray[105122],100))
您可以通过斜边的增量找到最近的笔划

如果数据距离较远,则可以删除最近的索引

这种方法返回给定数据的索引

函数查找(数组、坐标){
常数
getDelta=c=>c.map((v,i)=>v-坐标[i]),
map=array.reduce(
(m,{data},i)=>data.reduce((n,c)=>n.set(c,i),m),
新地图
),
c=[…map.keys()].reduce((a,b)=>Math.hypot(…getDelta(a))log(find(strokeArray[205595]);//strokeArray[2]
我正在检查X和Y坐标之间的差异是否小于先前匹配的循环。此外,我还存储了最小差异(阈值为2)结果索引和笔划数组索引。我们还可以计算x或y坐标的最近值

此外,我正在对象数组中存储最接近的潜在匹配(在阈值内)

let strokeArray=[{
身份证号码:1234,
数据:[
[101, 122],
[105, 124],
[107, 127],
[109, 129],
]
},
{
身份证号码:5678,
数据:[
[201, 522],
[205, 597],
[207, 527],
[210, 523],
]
},
{
id:0001,
数据:[
[601, 622],
[205, 595],
[607, 627],
[206, 594],
]
}
]
const[matchX,matchY]=[205595]//要匹配的反序列化值
var-diffX=0;
var-diffY=0;
var-prevX=2;
var-prevY=2;
var指数=0;
var outerIndex=0;
var resultArray=新数组;
var closestMatchesArray=新数组;
strokeArray.forEach((笔划,i)=>{
stroke.data.forEach(([coordinateX,coordinateY],index)=>{
diffX=Math.abs(coordinateX-matchX);
diffY=Math.abs(coordinateY-matchY);
如果(diffX另一种解决方案:

function getThatPoint(x: number, y: number, strokeData: any) {
  const rawCalcs = [].concat(...strokeData.map((sd: any) => {
    return sd.data.map((r: any) => {
      return { strokeId: sd.id, areaIndex: sd.data.indexOf(r), areaX: r[0], areaY: r[1], targetX: x, targetY: y, distance: Math.sqrt(Math.pow(r[0] - x, 2) + Math.pow(r[1] - y, 2)) };
    });
  })) as any[];
  return rawCalcs.filter(x => x.distance === Math.min(...rawCalcs.map((rc: any) => {
    return rc.distance;
  })))[0];
}

如果你在其他情况下搜索最接近的结果,为什么你想要一个空结果?这两个值是否匹配?这是静态的数据,还是在会话期间更新的数据?我的意思是可以添加/删除项目吗?@NinaScholz感谢你花时间阅读我的要求。这是针对移动应用程序的,实际上你在上面看到的数据是3笔画。Im正在尝试删除用户点击的笔划。由于可能有正负2个点,我需要最近的笔划。但是如果用户点击某个地方意味着我不需要最近的笔划。希望你明白我的意思。最近的笔划就像相等一样?你必须定义什么是“最近的”…在点系统中,我认为是模块中距离x-y坐标的距离。…因此,您可以检查| x-xCheck |+| y-yCheck |在您必须定义的范围内接近0(例如:10)…如果存在=>返回索引,则返回Null这会找到最近的笔划,但如果坐标远离,也会找到最近的笔划。谢谢您的回答!谢谢您的回答。这正是我需要的。!