I use a method in a javascript structure where the return value can be ANY of the following
one coordinate pair xy
[x,y]
array of xy coordinate pairs
[[x,y],[x,y],...]
array of arrays of xy coordinate pairs
[[[x,y],[x,y]],[[x,y],[x,y]],...]
The return value depends on the geometry of the object (single point, line, or several lines). Regardless of the return value and its depth of the array, I want to capture the first xy coordinate pair. What is an effective way to do this?
Here is my code to achieve the goal so far:
//here is the magic method that can return one of three things :) var mysteryCoordinates = geometry.getCoordinates(); var firstCoord; if(typeof mysteryCoordinates[0] === 'number') { firstCoord = mysteryCoordinates; } else if (typeof mysteryCoordinates[0][0] === 'number') { firstCoord = mysteryCoordinates[0]; } else if (typeof mysteryCoordinates[0][0][0] === 'number') { firstCoord = mysteryCoordinates[0][0]; }
I really hate this solution and am looking for something more elegant.
javascript arrays multidimensional-array
Jellyraptor
source share