[英]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.