Object.keys()
and Array.prototype.map()
to map each coordinate to its difference between the two points.Math.hypot()
to calculate the Euclidean distance between the two points.const euclideanDistance = (a, b) =>
Math.hypot(...Object.keys(a).map(k => b[k] - a[k]));
euclideanDistance([1, 1], [2, 3]); // ~2.2361 euclideanDistance([1, 1, 1], [2, 3, 2]); // ~2.4495
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️