I am using a function in a JavaScript framework where the return value can be ANY of the following
a single xy coordinate pair
[x,y]an array of xy coordinate pairs
[[x,y],[x,y],...]an 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 multiple lines). Regardless of the return value and its array depth, I want to grab the first xy coordinate pair. What is an efficient way to do this?
Here is my code to achieve the objective 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 a bit more elegant.
geometryobject not have some property telling you what type of geometry it represents, ie point,line,lines?