Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

hasOwnProperty() is only checking if a certain property exists in a JSON, but doesn't return anything if it doesn't

I keep trying different methods to check if this JSON contains "attributes." In this way I can determine if the given coordinates are outside of wetlands. If they are in wetlands, "attributes" will exist in the JSON. If they aren’t in wetlands, ‘attributes’ won’t be in the JSON.

When I run this function, I am only getting TRUE – when I type in coordinates that are in a wetland (try 43.088 instead, in the JSON url, which returns true).

However I want FALSE for the given url. For some reason when I do console.log("FALSE"), this doesn’t appear or return in the console at all if hasOwnProperty('attributes') == false.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Am I missing something?

function(GetData) {

  fetch('https://www.fws.gov/wetlandsmapservice/rest/services/Wetlands/MapServer/0/query?where=&text=&objectIds=&time=&geometry=-88.305%2C43.060&geometryType=esriGeometryPoint&inSR=4326&spatialRel=esriSpatialRelWithin&relationParam=&outFields=WETLAND_TYPE&returnGeometry=false&returnTrueCurves=false&maxAllowableOffset=&geometryPrecision=&outSR=&returnIdsOnly=false&returnCountOnly=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&returnZ=false&returnM=false&gdbVersion=&returnDistinctValues=false&resultOffset=&resultRecordCount=&queryByDistance=&returnExtentsOnly=false&datumTransformation=&parameterValues=&rangeValues=&f=pjson&__ncforminfo=qCOZOO8Kyr4uogGcKvxkzzuK7gmavd4CxwTAkdbAsF2_aT4eeNbB0NpLwCYwiAJSf1ZHqY3CKVZ3osgMevhYGQrqRUQZej5oHaSmnSIaiZZb469Cexv-zqqmgYMuFJAAzrcRxvKXPBz9VnYPnMrM6kBNhO-cz6yK_w5T1mqNu_VXSbjBSihVf4_mlUBSVb9yf4C8scYXWm9Iak2Nfn1dtJACNUHLBHSElLvc1wxFMO2eUWNsD3qpCk3kAcRyYftuFU86n7THyk2IvkIUpxNmDHRxmmbgSYvPLMkl8t41Jzjp_bntkIyOWB0u8cQU2VsfASFUdznRkvrvYrQxgR8eyvsPq5oV_ZoPSksVCew6xev0K_TV2NU-kjojYpowMVXpZtCX9P-Q_7m8ywt2PyLPhEVgQB12ji1S7G5FRzIt6E0SDoXMY1vqQvPtedaNYbBCazXgs05L9DFKdtvrwmQVCeLmpBTduIhF9Sk4kozMnFX6GOANrZJMCI9AssN0DjrhlZkgDVw0l1flF44Zli927CXGTQ-oUpwsn7PPypVkN2iDJf-nz9XNbj82sv1c6B5s5UZVwiOp8VHJfZSDJ8BAYR4z_oONT2JwbVSKKlFKeN72f-Y6EejcB9wPKmn5kYjv7CKkRyIIv4F4cqVWxLK9x33uvEDMTvxX')
    .then(function(response) {
      return response.json();
    })
    .then(function(data) {
      appendData3(data);
    })

    .catch(function(err) {
      console.log('error: ' + err);
    });


  function appendData3(data) {
    for (let obj of data['features']) {

      if (obj.hasOwnProperty('attributes') == false) {
        console.log("FALSE");
      } else {
        console.log("TRUE");
      }
    }
  }

};

>Solution :

The issue is that in the response data['features'] is empty. When iterating over an empty array, nothing within the for...of loop is executed.

const emptyArray = [];
for (const item of emptyArray) {
  // body is never executed...
}

If just checking the presence of an item within data['features'] is enough, you could use the length of the array.

function appendData3(data) {
  if (data.features.length > 0) {
    console.log("TRUE");
  } else {
    console.log("FALSE");
  }
}

To check if one of the elements has the property "attributes" you could use some():

function appendData3(data) {
  if (data.features.some(item => item.hasOwnProperty("attributes"))) {
    console.log("TRUE");
  } else {
    console.log("FALSE");
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading