拥有像data
这样的动态对象,请您告诉我有没有办法可以像这样映射数据:
我需要的是使用特定值geometry
和Transformer
对所有属性进行Service Point
,并将它们加载到数组中。
var transformers = [
[-88.17602806699995, 41.78431233100008],
[-88.17546081099994, 41.783341919000065]
]
或
var servicePoints = [
[-88.17599727899994, 41.78465526100007],
[-88.17595382899998, 41.78455803400004],
[-88.17582231499995, 41.78435312600004],
[-88.17561004899994, 41.78400533500074],
[-88.17557576699994, 41.78393182000008],
[-88.17535967199996, 41.78352876900004]
]
这是数据集
var data = [{
"displayFieldName": "",
"fieldAliases": {
"OBJECTID": "OBJECTID"
},
"fields": [{
"name": "OBJECTID",
"type": "esriFieldTypeOID",
"alias": "OBJECTID"
}],
"features": [{
"attributes": {
"OBJECTID": 649
}
},
{
"attributes": {
"OBJECTID": 665
}
},
{
"attributes": {
"OBJECTID": 762
}
}
]
},
{
"displayFieldName": "",
"fieldAliases": {
"display": "display",
"OBJECTID": "OBJECTID"
},
"geometryType": "esriGeometryPoint",
"spatialReference": {
"wkid": 4326,
"latestWkid": 4326
},
"fields": [{
"name": "display",
"type": "esriFieldTypeString",
"alias": "display",
"length": 50
},
{
"name": "OBJECTID",
"type": "esriFieldTypeOID",
"alias": "OBJECTID"
}
],
"features": [{
"attributes": {
"display": "Transformer",
"OBJECTID": 1537
},
"geometry": {
"x": -88.17602806699995,
"y": 41.78431233100008
}
},
{
"attributes": {
"display": "Transformer",
"OBJECTID": 1591
},
"geometry": {
"x": -88.17546081099994,
"y": 41.783341919000065
}
}
]
},
{
"displayFieldName": "",
"fieldAliases": {
"display": "display",
"OBJECTID": "OBJECTID"
},
"geometryType": "esriGeometryPoint",
"spatialReference": {
"wkid": 4326,
"latestWkid": 4326
},
"fields": [{
"name": "display",
"type": "esriFieldTypeString",
"alias": "display",
"length": 50
},
{
"name": "OBJECTID",
"type": "esriFieldTypeOID",
"alias": "OBJECTID"
}
],
"features": [{
"attributes": {
"display": "Service Point",
"OBJECTID": 13597
},
"geometry": {
"x": -88.17599727899994,
"y": 41.78465526100007
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13598
},
"geometry": {
"x": -88.17595382899998,
"y": 41.78455803400004
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13599
},
"geometry": {
"x": -88.17582231499995,
"y": 41.78435312600004
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13600
},
"geometry": {
"x": -88.17561004899994,
"y": 41.784005335000074
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13601
},
"geometry": {
"x": -88.17557576699994,
"y": 41.78393182000008
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13602
},
"geometry": {
"x": -88.17535967199996,
"y": 41.78352876900004
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13603
},
"geometry": {
"x": -88.17534426199995,
"y": 41.78340020400003
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13649
},
"geometry": {
"x": -88.17450698899995,
"y": 41.78350136200004
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13650
},
"geometry": {
"x": -88.17435162999999,
"y": 41.783597986000075
}
}
]
}
];
我已经尝试过了:
var transformers = data.map((obj) => obj.features.map(({attributes})=>attributes.display || "NONE"));
但这只会在没有几何体的不同数组中返回它们!
答案 0 :(得分:1)
您可以循环遍历data
中所有对象的所有功能,并随时累积结果:
function get(data, attributeType) {
return data.reduce(function(acc, obj) { // for each object obj in the array data
obj.features.forEach(function(feature) { // for each feature feature in the array features of obj
if(feature.attributes // if this feature has an attributes property
&& typeof feature.attributes === "object" // and that attributes property is an object
&& feature.attributes.display === attributeType) { // and the display property of this feature's attributes is the same as attributeType
acc.push([feature.geometry.x, feature.geometry.y]); // then push the coordinates of its geometry coordinates to acc
}
});
return acc;
}, []);
}
然后为了得到变形,你打电话就是这样:
var transformers = get(data, "Transformer");
并获得服务指针,你可以这样称呼它:
var servicePoints = get(data, "Service Point");
示例:强>
function get(data, attributeType) {
return data.reduce(function(acc, obj) { // for each object obj in the array data
obj.features.forEach(function(feature) { // for each feature feature in the array features of obj
if(feature.attributes // if this feature has an attributes property
&& typeof feature.attributes === "object" // and that attributes property is an object
&& feature.attributes.display === attributeType) { // and the display property of this feature's attributes is the same as attributeType
acc.push([feature.geometry.x, feature.geometry.y]); // then push the coordinates of its geometry coordinates to acc
}
});
return acc;
}, []);
}
var data = [{"displayFieldName":"","fieldAliases":{"OBJECTID":"OBJECTID"},"fields":[{"name":"OBJECTID","type":"esriFieldTypeOID","alias":"OBJECTID"}],"features":[{"attributes":{"OBJECTID":649}},{"attributes":{"OBJECTID":665}},{"attributes":{"OBJECTID":762}}]},{"displayFieldName":"","fieldAliases":{"display":"display","OBJECTID":"OBJECTID"},"geometryType":"esriGeometryPoint","spatialReference":{"wkid":4326,"latestWkid":4326},"fields":[{"name":"display","type":"esriFieldTypeString","alias":"display","length":50},{"name":"OBJECTID","type":"esriFieldTypeOID","alias":"OBJECTID"}],"features":[{"attributes":{"display":"Transformer","OBJECTID":1537},"geometry":{"x":-88.17602806699995,"y":41.78431233100008}},{"attributes":{"display":"Transformer","OBJECTID":1591},"geometry":{"x":-88.17546081099994,"y":41.783341919000065}}]},{"displayFieldName":"","fieldAliases":{"display":"display","OBJECTID":"OBJECTID"},"geometryType":"esriGeometryPoint","spatialReference":{"wkid":4326,"latestWkid":4326},"fields":[{"name":"display","type":"esriFieldTypeString","alias":"display","length":50},{"name":"OBJECTID","type":"esriFieldTypeOID","alias":"OBJECTID"}],"features":[{"attributes":{"display":"Service Point","OBJECTID":13597},"geometry":{"x":-88.17599727899994,"y":41.78465526100007}},{"attributes":{"display":"Service Point","OBJECTID":13598},"geometry":{"x":-88.17595382899998,"y":41.78455803400004}},{"attributes":{"display":"Service Point","OBJECTID":13599},"geometry":{"x":-88.17582231499995,"y":41.78435312600004}},{"attributes":{"display":"Service Point","OBJECTID":13600},"geometry":{"x":-88.17561004899994,"y":41.784005335000074}},{"attributes":{"display":"Service Point","OBJECTID":13601},"geometry":{"x":-88.17557576699994,"y":41.78393182000008}},{"attributes":{"display":"Service Point","OBJECTID":13602},"geometry":{"x":-88.17535967199996,"y":41.78352876900004}},{"attributes":{"display":"Service Point","OBJECTID":13603},"geometry":{"x":-88.17534426199995,"y":41.78340020400003}},{"attributes":{"display":"Service Point","OBJECTID":13649},"geometry":{"x":-88.17450698899995,"y":41.78350136200004}},{"attributes":{"display":"Service Point","OBJECTID":13650},"geometry":{"x":-88.17435162999999,"y":41.783597986000075}}]}];
var transformers = get(data, "Transformer");
var servicePoints = get(data, "Service Point");
console.log("Transformers:", transformers);
console.log("servicePoints:", servicePoints);
答案 1 :(得分:1)
可能这可以用更好的方式完成(map,filter,reduce ...?),但是两个forEach()循环也可以完成这项任务:
transformers=[];
service_points=[];
data.forEach(function(el) {
el.features.forEach(function(e) {
if(e.attributes.display) {
if(e.attributes.display=='Transformer') {
transformers.push([e.geometry.x,e.geometry.y]);
}
if(e.attributes.display=='Service Point') {
service_points.push([e.geometry.x,e.geometry.y]);
}
}
});
});
console.log(transformers,service_points);
演示:
var data = [{
"displayFieldName": "",
"fieldAliases": {
"OBJECTID": "OBJECTID"
},
"fields": [{
"name": "OBJECTID",
"type": "esriFieldTypeOID",
"alias": "OBJECTID"
}],
"features": [{
"attributes": {
"OBJECTID": 649
}
},
{
"attributes": {
"OBJECTID": 665
}
},
{
"attributes": {
"OBJECTID": 762
}
}
]
},
{
"displayFieldName": "",
"fieldAliases": {
"display": "display",
"OBJECTID": "OBJECTID"
},
"geometryType": "esriGeometryPoint",
"spatialReference": {
"wkid": 4326,
"latestWkid": 4326
},
"fields": [{
"name": "display",
"type": "esriFieldTypeString",
"alias": "display",
"length": 50
},
{
"name": "OBJECTID",
"type": "esriFieldTypeOID",
"alias": "OBJECTID"
}
],
"features": [{
"attributes": {
"display": "Transformer",
"OBJECTID": 1537
},
"geometry": {
"x": -88.17602806699995,
"y": 41.78431233100008
}
},
{
"attributes": {
"display": "Transformer",
"OBJECTID": 1591
},
"geometry": {
"x": -88.17546081099994,
"y": 41.783341919000065
}
}
]
},
{
"displayFieldName": "",
"fieldAliases": {
"display": "display",
"OBJECTID": "OBJECTID"
},
"geometryType": "esriGeometryPoint",
"spatialReference": {
"wkid": 4326,
"latestWkid": 4326
},
"fields": [{
"name": "display",
"type": "esriFieldTypeString",
"alias": "display",
"length": 50
},
{
"name": "OBJECTID",
"type": "esriFieldTypeOID",
"alias": "OBJECTID"
}
],
"features": [{
"attributes": {
"display": "Service Point",
"OBJECTID": 13597
},
"geometry": {
"x": -88.17599727899994,
"y": 41.78465526100007
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13598
},
"geometry": {
"x": -88.17595382899998,
"y": 41.78455803400004
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13599
},
"geometry": {
"x": -88.17582231499995,
"y": 41.78435312600004
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13600
},
"geometry": {
"x": -88.17561004899994,
"y": 41.784005335000074
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13601
},
"geometry": {
"x": -88.17557576699994,
"y": 41.78393182000008
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13602
},
"geometry": {
"x": -88.17535967199996,
"y": 41.78352876900004
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13603
},
"geometry": {
"x": -88.17534426199995,
"y": 41.78340020400003
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13649
},
"geometry": {
"x": -88.17450698899995,
"y": 41.78350136200004
}
},
{
"attributes": {
"display": "Service Point",
"OBJECTID": 13650
},
"geometry": {
"x": -88.17435162999999,
"y": 41.783597986000075
}
}
]
}
];
transformers=[];
service_points=[];
data.forEach(function(el) {
el.features.forEach(function(e) {
if(e.attributes.display) {
if(e.attributes.display=='Transformer') {
transformers.push([e.geometry.x,e.geometry.y]);
}
if(e.attributes.display=='Service Point') {
service_points.push([e.geometry.x,e.geometry.y]);
}
}
});
});
console.log(transformers,service_points);