简体   繁体   中英

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

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.

As you have correctly pointed in your question, you can use Array.prototype.map for this task. All you have to do is use map for the features array of your object as shown below:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM