簡體   English   中英

從動態對象映射並生成數組

[英]Mapping and generating an array from dynamic object

我有一個像這樣的動態物體

[  
   {  
      "displayFieldName":"",
      "fieldAliases":{  
         "OBJECTID":"OBJECTID"
      },
      "geometryType":"esriGeometryPoint",
      "spatialReference":{  
         "wkid":102100,
         "latestWkid":3857
      },
      "fields":[  
         {  
            "name":"OBJECTID",
            "type":"esriFieldTypeOID",
            "alias":"OBJECTID"
         }
      ],
      "features":[  
         {  
            "attributes":{  
               "OBJECTID":5270
            },
            "geometry":{  
               "x":-9814184.757,
               "y":5130582.574600004
            }
         },
         {  
            "attributes":{  
               "OBJECTID":5271
            },
            "geometry":{  
               "x":-9814152.5879,
               "y":
            }
         },
         {  
            "attributes":{  
               "OBJECTID":5272
            },
            "geometry":{  
               "x":-9814147.7353,
               "y":5130632.882600002
            }
         },
         ...
         ]
     }
]

如何從幾何節點中獲取x和Y並將它們加載到新數組中,就像

var points =  ['-9814184.757,5130582.574600004',
               '-9814152.5879, 5130624.636799999', 
                '-9814147.7353,5130632.882600002',
              ...,
              ]

我已經嘗試過了

map = data.map(a => a.geometry.x, a => a.geometry.x);
console.log(map);`

但這只是將x值添加到數組中。

正如您已正確指出問題一樣,可以將Array.prototype.map用於此任務。 您所要做的就是使用map作為對象的features數組,如下所示:

/* Use 'map' to concatenate together each geometry value-pair into a string. */
var points = features.map((obj)=>obj.geometry.x + ", " + obj.geometry.y);

作為替代方案,您可以將每個對放在子數組中,並對上面的代碼稍作調整:

/* Use 'map' to put each geometry value-pair into an array. */
var points = features.map((obj)=>[obj.geometry.x, obj.geometry.y]);

請查看下面的代碼片段,以查看完整的代碼。

片段:

 /* The 'features' array of your object. */ var features = [ { "attributes":{ "OBJECTID":5270 }, "geometry":{ "x":-9814184.757, "y":5130582.574600004 } }, { "attributes":{ "OBJECTID":5271 }, "geometry":{ "x":-9814152.5879, "y": 5130624.636799999 } }, { "attributes":{ "OBJECTID":5272 }, "geometry":{ "x":-9814147.7353, "y":5130632.882600002 } } ] /* Use 'map' to concatenate together each geometry value-pair into a string. */ var points1 = features.map((obj)=>obj.geometry.x + ", " + obj.geometry.y); /* Use 'map' to put each geometry value-pair into an array. */ var points2 = features.map((obj)=>[obj.geometry.x, obj.geometry.y]); /* Log the results. */ console.log("as strings: ", points1); console.log("as sub-arrays: ", points2); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM