gpt4 book ai didi

javascript - 如何通过选择对象中的特定属性来使用 map 功能

转载 作者:行者123 更新时间:2023-11-29 20:58:53 24 4
gpt4 key购买 nike

我有一个像这样的动态对象 data。有什么方法可以像这样映射数据吗?

我需要的是用 TransformerService Point 的特定值映射所有属性的 geometry 并将它们加载到数组中。

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"));

但这只会在没有几何形状的不同数组中返回它们!

最佳答案

您可以遍历 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;
}, []);
}

然后要获取转换,您可以这样调用 get:

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

关于javascript - 如何通过选择对象中的特定属性来使用 map 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47777548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com