JS
\nconst nearestValidPoint = (x, y, points) => {\n    const result = { distance: Infinity, index: -1 }\n    for (let i = 0; i < points.length; i++) {\n        if
```\nconst nearestValidPoint = (x, y, points) => {\n    const result = { distance: Infinity, index: -1 }\n    for (let i = 0; i < points.length; i++) {\n        if (points[i][0] === x || points[i][1] === y) {\n            const calc = Math.abs(x - points[i][0]) + Math.abs(y - points[i][1])\n            if (calc < result.distance) {\n                result.distance = calc\n                result.index = i\n            }\n        }\n    }\n    return result.index\n}\n```
[]