简体   繁体   English

从动态对象映射并生成数组

[英]Mapping and generating an array from dynamic object

I have a dynamic object like this 我有一个像这样的动态物体

[  
   {  
      "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
            }
         },
         ...
         ]
     }
]

How can I grab x and Y from the geometry nodes and load them to new array to be like 如何从几何节点中获取x和Y并将它们加载到新数组中,就像

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

I already tried this 我已经尝试过了

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

but it is just adding the x values into array. 但这只是将x值添加到数组中。

As you have correctly pointed in your question, you can use Array.prototype.map for this task. 正如您已正确指出问题一样,可以将Array.prototype.map用于此任务。 All you have to do is use map for the features array of your object as shown below: 您所要做的就是使用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);

As an alternative, you can put each pair in sub-array with a little tweak to the code above: 作为替代方案,您可以将每个对放在子数组中,并对上面的代码稍作调整:

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

Check out the snippet below to see the full code in action. 请查看下面的代码片段,以查看完整的代码。

Snippet: 片段:

 /* 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