0

How I can check if a Mapped object is empty or not? Basically what I need to do is checking if there is any Array of Fuses in the data? If not, then do something else.

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

9
  • 1
    Can you simplify your code/data structure to make it more readable for us? What do you want to test exactly? Commented Dec 14, 2017 at 1:28
  • Ok here I am loading geometry of a big data set to different collections based on attribute name. Now there situations that I am getting zero value for Fuses or for Transformers now how I can check if there are no values for them? Commented Dec 14, 2017 at 1:31
  • So the data does not have any where display === 'Fuses', is that what you're trying to check? Commented Dec 14, 2017 at 1:34
  • (e.attributes.display == 'Fuses').length == 0 this line doesn't make sense, value inside braces is boolean and doesn't have a length property Commented Dec 14, 2017 at 1:36
  • 1
    So this is using ArcGIS for JavaScript API, right? You’re getting data from a Feature Service as well it looks like. Typically the approach is to loop through the Features in a FestureSet to investigate attributes. Sounds like you just need a loop that checks the display attribute to see if it === ‘Fuses’. If so, do something. If not do something else. What is the error you are encountering? Commented Dec 14, 2017 at 1:43

2 Answers 2

1

The function below will build up an object of arrays (one for fuses, one for service point, and one for transformer) using reduce, and push the coords in as it goes. When it is done, you can easily check to see if the fuses array .length === 0, and if so, do what you need to

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

Sign up to request clarification or add additional context in comments.

Comments

1

If you need to check the fuses for each data element it can be done as below

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.