Reputation: 6316
I have a dynamic object like this data
. Is there is any way that I can map data like this?
What I need is mapping geometry
of all attributes with specific values of Transformer
and Service Point
and load them into an array.
var transformers = [
[-88.17602806699995, 41.78431233100008],
[-88.17546081099994, 41.783341919000065]
]
or
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]
]
Here is the dataset
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
}
}
]
}
];
I already tried this:
var transformers = data.map((obj) => obj.features.map(({attributes})=>attributes.display || "NONE"));
but this only returns them in different arrays without geometry!
Upvotes: 2
Views: 566
Reputation: 11328
Probably this can be done on fancier way (map, filter, reduce...?), but two forEach() loops will do the job, too:
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);
Demo:
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);
Upvotes: 1
Reputation: 31712
You can loop over all features of all objects in data
and accumulate the results as you go:
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;
}, []);
}
Then to get transformes, you call get like this:
var transformers = get(data, "Transformer");
and to get service pointes, you call it like this:
var servicePoints = get(data, "Service Point");
Example:
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);
Upvotes: 1