的數組編號的第一陣列我正在使用一個javascript框架的方法,其中所述返回值可以是以下獲取可變深度
單個XY的ANY座標對
[x,y]
XY陣列座標對
[[x,y],[x,y],...]
XY的數組的數組座標對
[[[x,y],[x,y]],[[x,y],[x,y]],...]
返回值取決於對象(單點,線,或多行)的幾何形狀。無論返回值和數組深度如何,我都想抓住第一個xy座標對。什麼是有效的方法來做到這一點?
這裏是我的代碼,實現迄今目標:
//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];
}
我真的很討厭這個解決方案,並正在尋找的東西多一點優雅。
是否'geometry'對象沒有的一些特性告訴你它代表什麼類型的幾何形狀,即點,線,線? –
__我想抓住第一個xy座標對___你只對第一對有興趣?或者稍後您可能還需要其他一些索引數據? –
@PatrickEvans它的確如此,但我必須對幾何對象進行三次單獨的檢查,以瞭解訪問第一個座標對的適當深度。 – JellyRaptor