JavaScript Solution Using a Map Object
Found this solution helpful? Consider showing support by upvoting this post. If there are any questions, kindly leave a comment below. Thank you and happy hacki
Found this solution helpful? Consider showing support by upvoting this post. If there are any questions, kindly leave a comment below. Thank you and happy hacking!\n```\nvar nearestValidPoint = function (x, y, points) {\n    let manhattanIndices = new Map()\n    let currentMin = Infinity\n\n    points.forEach((point, i) => {\n        if (point[0] === x || point[1] === y) {\n            manhattanIndices.set(i, Math.abs(x - point[0]) + Math.abs(y - point[1]))\n            currentMin = Math.min(currentMin, Math.abs(x - point[0]) + Math.abs(y - point[1]))\n        }\n    })\n\n    for(let [index, distance] of manhattanIndices){\n        if(distance === currentMin){\n            return index\n        }\n    }\n\n    return -1\n};\n```
['JavaScript']