gpt4 book ai didi

javascript - 在 JavaScript 中映射对象

转载 作者:行者123 更新时间:2023-11-30 20:54:59 24 4
gpt4 key购买 nike

为什么我无法在以下代码中映射数组/对象中的显示名称?

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 points1 = data.map((obj) => obj.features.attributes.display);
console.log(points1);

最佳答案

features 数组上使用另一个 map 。真的不清楚预期结果是什么,因为有些没有 display 并且有很多重复

var points1 = data.map((obj) => obj.features.map(({attributes})=>attributes.display || "NONE"));
console.log(points1);
<script>
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
}
}
]
}
];


</script>

关于javascript - 在 JavaScript 中映射对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47763331/

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