All of KIKO Software's review applies, but the suggest replacement code doesn't work on the JavaScript implementation being targeted (ArcGIS Arcade), because it doesn't support these keywords:
const
let
for (… of …)
It supports only the keywords mentioned in ArcGIS Arcade - Structure and Logic.
We can modify KIKO Software's code to something compatible:
function pythagoras(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
}
var paths = [[[0, 5, null], [10, 10, null], [30, 0, null], [50, 10, null], [60, 10, null]]];
var oldX = paths[0][0][0], oldY = paths[0][0][1], length = 0;
for (var path_idx in paths) {
for (var point_idx in paths[path_idx]) {
var newX = paths[path_idx][point_idx][0], newY = paths[path_idx][point_idx][1]
length += pythagoras(oldX, oldY, newX, newY);
paths[path_idx][point_idx][2] = length;
oldX = newX;
oldY = newY;
}
}
console.log(JSON.stringify(paths));
//Output: [[[0,5,0],[10,10,11.180339887498949],[30,0,33.54101966249685],[50,10,55.90169943749475],[60,10,65.90169943749476]]]
I would be happy to hear comments about anything I might have overlooked.
Related:
I have a related question in the ArcGIS Community that has a GIS-specific script: Set polyline M-values to cumulative length of lineSet polyline M-values to cumulative length of line.
That script is the latest version.