您能否请看下面的代码片段,让我知道如何检查Mapped对象是否为空?
基本上我需要做的是检查data
中是否有任何保险丝阵列?如果不?然后再做一些事情
if(e.attributes.display=='Fuses') {
if((e.attributes.display == 'Fuses').length == 0){
console.log("No Data For Fuses");
}else{
fuses.push([e.geometry.x,e.geometry.y]);
}
}
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=[];
fuses=[];
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=='Fuses') {
if((e.attributes.display == 'Fuses').length == 0){
console.log("No Data For Fuses");
}else{
fuses.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, fuses);
答案 0 :(得分:1)
下面的函数将使用reduce建立一个数组对象(一个用于保险丝,一个用于服务点,一个用于变换器),并按原样推入线圈。完成后,您可以轻松检查保险丝阵列.length === 0
,如果是,请执行您需要的操作
function process() {
data.forEach((el) => {
let arrays = el.features.reduce(function(result, feature) {
if (feature.attributes && feature.attributes.display) {
// Get the display name to use as the key for our object
let key = feature.attributes.display.replace(" ", "")
// Push the coords into the array
result[key].push([feature.geometry.x, feature.geometry.y]);
}
return result;
}, {
// Provide a default object with empty arrays
Transformer: [],
Fuses: [],
ServicePoint: []
});
console.log(arrays);
if (arrays.Fuses.length === 0) {
// Do what you need to here.
console.log("No fuses");
}
});
}
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
}
}
]
}
];
process();
答案 1 :(得分:1)
如果需要检查每个数据元素的保险丝,可按以下步骤进行操作
data.forEach(function(el) {
var hasFuses = el.features.some((e) => e.attributes.display === 'Fuses');
console.log('has: ' + hasFuses);
el.features.forEach(function(e) {
if (!hasFuses) {
console.log("No Data For Fuses");
}
switch (e.attributes.display) {
case 'Transformer':
transformers.push([e.geometry.x, e.geometry.y]);
break;
case 'Fuses':
fuses.push([e.geometry.x, e.geometry.y]);
break;
case 'Service Point':
service_points.push([e.geometry.x, e.geometry.y]);
break;
}
});
});
console.log(transformers, service_points, fuses);